tags:

views:

31

answers:

1

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="[email protected]" 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
+1  A: 

You open for some jQuery?

$().ready(function(){
    $('.DefaultTextBox:eq(0)').focus();
});
Tim B James
If the OP is running .Net 4.0 then he can set the clientidmode to static and access it like $('#CurrentPassword').focus();
Chris Lively
also with the .Net 4.0 ChangePassword control, the HTML is nicer :)
Tim B James
I'm not a JQuery guy yet, but I thought I would give it a shot. See my updated question.
Velika