2010-10-13 3 views
0

Lorsque le formulaire est affiché, je souhaite définir le focus sur la zone de texte CurrentPassword. Quelle est la manière la plus simple de faire ça? Y a-t-il un moyen d'y accéder directement en utilisant son identifiant? Apparemment, parce qu'il est imbriqué, référencer le contrôle par son ID résulte en une variable non reconnue. Sinon, je pourrais écrire une déclaration FindControl laid (certains autres) comme ceci:Meilleure façon de référencer Textbox imbriqué dans un contrôle ASP.NET ChangePassword (ou autre?)

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

mais, en plus d'être laid, cela semble fragile aux changements de l'interface graphique.

Je pourrais aussi écrire une instruction FindControl récursive, mais si elle est pratique, elle est moins efficace qu'une référence plus directe.

Suggestions, Communauté 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="[email protected]" IsBodyHtml="True" Subject="Your password has changed"> 
     </MailDefinition> 
    </asp:ChangePassword> 
</asp:Content> 

Sur la base de la réponse ci-dessous, j'ai changé le ClientIDMode du CurrentPassword Textbox « statique. » J'ai ensuite ajouté ce qui suit montre après la première ligne préexistante:

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

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

Mais je suis l'erreur suivante, même erreur quand je décommenté la première suggestion.

Microsoft JScript runtime error: Object expected 
+0

Avez-vous ajouté une référence au script jQuery?

Répondre

1

Vous ouvrez pour certains jQuery?

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

Si l'OP exécute .Net 4.0 alors il peut mettre le clientidmode en statique et y accéder comme $ ('# CurrentPassword'). focus(); – NotMe

+0

également avec le contrôle ChangePassword. Net 4.0, le HTML est plus agréable :) –

+0

Je ne suis pas encore un mec JQuery, mais j'ai pensé que je pourrais essayer. Voir ma question mise à jour. – ChadD

0

Ceci est une vieille question, mais je poste la manière de référencer C# les bonnes zones de texte au cas où quelqu'un le regarderait.

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