Best way to reference Textbox nested in a ChangePassword (or other?) ASP.NET control
<p>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:</p>

<pre><code>ChangePassword1.Controls(1).Controls(0).Controls(0).Controls(0).Controls...etc...FindControl("CurrentPassword")
</code></pre>

<p>but, besides being ugly, this seems brittle to GUI changes.</p>

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

<p>Suggestions, Ol' Wise Community?  </p>

<pre><code>&lt;%@ Page Title="" Language="VB" MasterPageFile="~/Master Pages/MasterPage.master" AutoEventWireup="false" CodeFile="ChangePassword.aspx.vb" Inherits="ChangePassword" %&gt;

&lt;asp:Content ID="Content1" ContentPlaceHolderID="head" runat="Server"&gt;
&lt;/asp:Content&gt;
&lt;asp:Content ID="Content2" runat="server" ContentPlaceHolderID="phPageContent"&gt;
    &lt;h1 style="text-align: left;"&gt;
        Change Password
    &lt;/h1&gt;
    &lt;hr /&gt;
    &lt;asp:ChangePassword ID="ChangePassword1" runat="server"&gt;
        &lt;ChangePasswordTemplate&gt;
            &lt;table cellpadding="1" cellspacing="0" style="border-collapse: collapse;"&gt;
                &lt;tr&gt;
                    &lt;td&gt;
                        &lt;table cellpadding="2"&gt;
                            &lt;tr&gt;
                                &lt;td align="right"&gt;
                                    &lt;asp:Label ID="CurrentPasswordLabel" runat="server" AssociatedControlID="CurrentPassword" CssClass="CaptionLabel"&gt;Current Password:&lt;/asp:Label&gt;
                                &lt;/td&gt;
                                &lt;td&gt;
                                    &lt;asp:TextBox ID="CurrentPassword" runat="server" CssClass="DefaultTextBox" TextMode="Password"&gt;&lt;/asp:TextBox&gt;
                                    &lt;asp:RequiredFieldValidator ID="CurrentPasswordRequired" runat="server" ControlToValidate="CurrentPassword" ErrorMessage="Password is required." ToolTip="Password is required." ValidationGroup="ChangePassword1"&gt;*&lt;/asp:RequiredFieldValidator&gt;
                                &lt;/td&gt;
                            &lt;/tr&gt;
                            &lt;tr&gt;
                                &lt;td align="right"&gt;
                                    &lt;asp:Label ID="NewPasswordLabel" runat="server" AssociatedControlID="NewPassword" CssClass="CaptionLabel"&gt;New Password:&lt;/asp:Label&gt;
                                &lt;/td&gt;
                                &lt;td&gt;
                                    &lt;asp:TextBox ID="NewPassword" runat="server" CssClass="DefaultTextBox" TextMode="Password"&gt;&lt;/asp:TextBox&gt;
                                    &lt;asp:RequiredFieldValidator ID="NewPasswordRequired" runat="server" ControlToValidate="NewPassword" ErrorMessage="New Password is required." ToolTip="New Password is required." ValidationGroup="ChangePassword1"&gt;*&lt;/asp:RequiredFieldValidator&gt;
                                &lt;/td&gt;
                            &lt;/tr&gt;
                            &lt;tr&gt;
                                &lt;td align="right"&gt;
                                    &lt;asp:Label ID="ConfirmNewPasswordLabel" runat="server" AssociatedControlID="ConfirmNewPassword" CssClass="CaptionLabel"&gt;Confirm New Password:&lt;/asp:Label&gt;
                                &lt;/td&gt;
                                &lt;td&gt;
                                    &lt;asp:TextBox ID="ConfirmNewPassword" runat="server" CssClass="DefaultTextBox" TextMode="Password"&gt;&lt;/asp:TextBox&gt;
                                    &lt;asp:RequiredFieldValidator ID="ConfirmNewPasswordRequired" runat="server" ControlToValidate="ConfirmNewPassword" ErrorMessage="Confirm New Password is required." ToolTip="Confirm New Password is required." ValidationGroup="ChangePassword1"&gt;*&lt;/asp:RequiredFieldValidator&gt;
                                &lt;/td&gt;
                            &lt;/tr&gt;
                            &lt;tr&gt;
                                &lt;td align="center" colspan="2"&gt;
                                    &lt;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"&gt;&lt;/asp:CompareValidator&gt;
                                &lt;/td&gt;
                            &lt;/tr&gt;
                            &lt;tr&gt;
                                &lt;td align="center" colspan="2" style="color: Red;"&gt;
                                    &lt;asp:Literal ID="FailureText" runat="server" EnableViewState="False"&gt;&lt;/asp:Literal&gt;
                                &lt;/td&gt;
                            &lt;/tr&gt;
                            &lt;tr&gt;
                                &lt;td align="right"&gt;
                                    &lt;asp:Button ID="ChangePasswordPushButton" runat="server" CommandName="ChangePassword" Text="Submit" ValidationGroup="ChangePassword1" /&gt;
                                &lt;/td&gt;
                                &lt;td&gt;
                                    &lt;asp:Button ID="CancelPushButton" runat="server" CausesValidation="False" CommandName="Cancel" Text="Cancel" /&gt;
                                &lt;/td&gt;
                            &lt;/tr&gt;
                        &lt;/table&gt;
                    &lt;/td&gt;
                &lt;/tr&gt;
            &lt;/table&gt;
        &lt;/ChangePasswordTemplate&gt;
        &lt;MailDefinition BodyFileName="~/EmailTemplates/ChangePassword.htm" From="info-noreply@pandavisa.com" IsBodyHtml="True" Subject="Your password has changed"&gt;
        &lt;/MailDefinition&gt;
    &lt;/asp:ChangePassword&gt;
&lt;/asp:Content&gt;
</code></pre>

<p>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:</p>

<pre><code>&lt;asp:Content ID="Content2" runat="server" ContentPlaceHolderID="phPageContent"&gt;

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

<p>But I got the following error, same error when I uncommented the first suggestion.</p>

<pre><code>Microsoft JScript runtime error: Object expected
</code></pre>
