开发者

Best way to reference Textbox nested in a ChangePassword (or other?) ASP.NET control

开发者 https://www.devze.com 2023-01-20 08:24 出处:网络
When the form is displayed, I\'d like to set focus to the CurrentPassword textbox. What is the easiest way to do this? Is there a way to access it directly using its ID? Apparently, because it is nest

When the form is displayed, I'd like to set focus to the CurrentPassword textbox. What is the easiest way to do this? Is there a way to access it directly using its ID? Apparently, because it is nested, referencing the control by its ID results in an unrecognized variable. Alternatively, I could write an ugly FindControl statement (some like) like this:

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

but, besides being ugly, this seems brittle to GUI changes.

I could also write a recursive FindControl statement, but while it is convenient, it is less efficient than a more direct reference.

Suggestions, Ol' Wise Community?

<%@ 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>

Based on the reply below, I changed the ClientIdMode of the CurrentPassword Textbox to "Static." I then added the following shown after the pre-existing first line:

<asp:Content ID="Content2" runat="server" ContentPlaceHolderID="phPageContent">

    <script type="text/javascript">
        $().ready(function () {
            //$('.DefaultTextBox:eq(0)').focus();
            $('#CurrentPassword').focus();
        });
    </script>

But I got the following error, same error when I uncommented the first suggestion.

Microsoft JScript runtime error: Object expected


You open for some jQuery?

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


This is an old question, but I'm posting the C# way of referencing the correct textboxes in case anybody looks at this.

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

精彩评论

暂无评论...
验证码 换一张
取 消