引用嵌套在ChangePassword(或其他?)ASP.NET控件中的Textbox的最佳方法

时间:2010-10-13 21:08:02

标签: asp.net

显示表单时,我想将焦点设置为CurrentPassword文本框。最简单的方法是什么?有没有办法直接使用其ID访问它?显然,因为它是嵌套的,所以通过其ID引用控件会导致无法识别的变量。或者,我可以写一个丑陋的FindControl语句(有些像):

ChangePassword1.Controls(1).Controls(0).Controls(0).Controls(0).Controls...etc...FindControl("CurrentPassword")

但是,除了丑陋之外,这对GUI的变化似乎很脆弱。

我也可以写一个递归的FindControl语句,虽然它很方便,但它的效率低于更直接的引用。

建议,Ol'Wise社区?

<%@ 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="info-noreply@pandavisa.com" IsBodyHtml="True" Subject="Your password has changed">
        </MailDefinition>
    </asp:ChangePassword>
</asp:Content>

根据下面的回复,我将CurrentPassword文本框的ClientIdMode更改为“静态”。然后我在预先存在的第一行之后添加了以下内容:

<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

2 个答案:

答案 0 :(得分:1)

你打开一些jQuery吗?

$().ready(function(){
    $('.DefaultTextBox:eq(0)').focus();
});

答案 1 :(得分:0)

这是一个老问题,但我发布了引用正确文本框的C#方式,以防有人看到这个。

TextBox txtCurrentPassword = (TextBox)changepasswordCtl.ChangePasswordTemplateContainer.FindControl("CurrentPassword");
TextBox txtNewPassword = (TextBox)changepasswordCtl.ChangePasswordTemplateContainer.FindControl("NewPassword");
TextBox txtConfirmNewPassword = (TextBox)changepasswordCtl.ChangePasswordTemplateContainer.FindControl("ConfirmNewPassword");