當顯示錶單時,我想將焦點設置爲CurrentPassword文本框。什麼是最簡單的方法來做到這一點?有沒有辦法直接使用其ID來訪問它?顯然,因爲它是嵌套的,所以通過它的ID引用控件導致一個無法識別的變量。或者,我可以寫一個醜陋的FindControl聲明(有些喜歡)這樣的:引用嵌入在ChangePassword(或其他?)ASP.NET控件中的文本框的最佳方法
ChangePassword1.Controls(1).Controls(0).Controls(0).Controls(0).Controls...etc...FindControl("CurrentPassword")
但是,除了是醜陋的,這似乎脆性GUI的變化。
我也可以寫一個遞歸的FindControl語句,但雖然很方便,但它比直接引用的效率低。
建議,奧智者社區?
<%@ Page Title="" Language="VB" MasterPageFile="~/Master Pages/MasterPage.master" AutoEventWireup="false" CodeFile="ChangePassword.aspx.vb" Inherits="ChangePassword" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="Server">
</asp:Content>
<asp:Content ID="Content2" runat="server" ContentPlaceHolderID="phPageContent">
<h1 style="text-align: left;">
Change Password
</h1>
<hr />
<asp:ChangePassword ID="ChangePassword1" runat="server">
<ChangePasswordTemplate>
<table cellpadding="1" cellspacing="0" style="border-collapse: collapse;">
<tr>
<td>
<table cellpadding="2">
<tr>
<td align="right">
<asp:Label ID="CurrentPasswordLabel" runat="server" AssociatedControlID="CurrentPassword" CssClass="CaptionLabel">Current Password:</asp:Label>
</td>
<td>
<asp:TextBox ID="CurrentPassword" runat="server" CssClass="DefaultTextBox" TextMode="Password"></asp:TextBox>
<asp:RequiredFieldValidator ID="CurrentPasswordRequired" runat="server" ControlToValidate="CurrentPassword" ErrorMessage="Password is required." ToolTip="Password is required." ValidationGroup="ChangePassword1">*</asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td align="right">
<asp:Label ID="NewPasswordLabel" runat="server" AssociatedControlID="NewPassword" CssClass="CaptionLabel">New Password:</asp:Label>
</td>
<td>
<asp:TextBox ID="NewPassword" runat="server" CssClass="DefaultTextBox" TextMode="Password"></asp:TextBox>
<asp:RequiredFieldValidator ID="NewPasswordRequired" runat="server" ControlToValidate="NewPassword" ErrorMessage="New Password is required." ToolTip="New Password is required." ValidationGroup="ChangePassword1">*</asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td align="right">
<asp:Label ID="ConfirmNewPasswordLabel" runat="server" AssociatedControlID="ConfirmNewPassword" CssClass="CaptionLabel">Confirm New Password:</asp:Label>
</td>
<td>
<asp:TextBox ID="ConfirmNewPassword" runat="server" CssClass="DefaultTextBox" TextMode="Password"></asp:TextBox>
<asp:RequiredFieldValidator ID="ConfirmNewPasswordRequired" runat="server" ControlToValidate="ConfirmNewPassword" ErrorMessage="Confirm New Password is required." ToolTip="Confirm New Password is required." ValidationGroup="ChangePassword1">*</asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td align="center" colspan="2">
<asp:CompareValidator ID="NewPasswordCompare" runat="server" ControlToCompare="NewPassword" ControlToValidate="ConfirmNewPassword" Display="Dynamic" ErrorMessage="The Confirm New Password must match the New Password entry." ValidationGroup="ChangePassword1"></asp:CompareValidator>
</td>
</tr>
<tr>
<td align="center" colspan="2" style="color: Red;">
<asp:Literal ID="FailureText" runat="server" EnableViewState="False"></asp:Literal>
</td>
</tr>
<tr>
<td align="right">
<asp:Button ID="ChangePasswordPushButton" runat="server" CommandName="ChangePassword" Text="Submit" ValidationGroup="ChangePassword1" />
</td>
<td>
<asp:Button ID="CancelPushButton" runat="server" CausesValidation="False" CommandName="Cancel" Text="Cancel" />
</td>
</tr>
</table>
</td>
</tr>
</table>
</ChangePasswordTemplate>
<MailDefinition BodyFileName="~/EmailTemplates/ChangePassword.htm" From="[email protected]" IsBodyHtml="True" Subject="Your password has changed">
</MailDefinition>
</asp:ChangePassword>
</asp:Content>
根據下面的回覆,我將CurrentPassword Textbox的ClientIdMode更改爲「Static」。然後我加入了預先存在的第一行後,以下圖所示:
<asp:Content ID="Content2" runat="server" ContentPlaceHolderID="phPageContent">
<script type="text/javascript">
$().ready(function() {
//$('.DefaultTextBox:eq(0)').focus();
$('#CurrentPassword').focus();
});
</script>
但我得到了下面的錯誤,同樣的錯誤,當我註釋掉的第一個建議。
Microsoft JScript runtime error: Object expected
您是否添加了對jQuery腳本的引用? – 2010-10-14 08:03:51