2011-12-29 90 views
0

我正在使用asp.net應用程序中的內置註冊模塊註冊新用戶。 我正在嘗試爲用戶設置用戶註冊時的角色,但由於某些原因,我無法訪問後端代碼中的文本框控件以將新用戶添加到所選角色,我正在嘗試使用此功能查找文本框代碼太多,但它返回null,這意味着控制不能發現:將用戶添加到註冊時的角色

TextBox txtUsername = (TextBox)Page.FindControl("UserName"); 
      Roles.AddUserToRole(txtUsername.Text, "User"); 

而且,這裏是寄存器模塊的HTML標記:

<asp:CreateUserWizardStep ID="RegisterUserWizardStep" runat="server"> 
       <ContentTemplate> 
        <h2> 
         Create a New Account 
        </h2> 
        <p> 
         Use the form below to create a new account. 
        </p> 
        <p> 
         Passwords are required to be a minimum of <%= Membership.MinRequiredPasswordLength %> characters in length. 
        </p> 
        <span class="failureNotification"> 
         <asp:Literal ID="ErrorMessage" runat="server"></asp:Literal> 
        </span> 
        <asp:ValidationSummary ID="RegisterUserValidationSummary" runat="server" CssClass="failureNotification" 
         ValidationGroup="RegisterUserValidationGroup"/> 
        <div class="accountInfo"> 
         <fieldset class="register"> 
          <legend>Account Information</legend> 
          <p> 
           <asp:Label ID="UserNameLabel" runat="server" AssociatedControlID="UserName">User Name:</asp:Label> 
           <asp:TextBox ID="UserName" runat="server" CssClass="textEntry"></asp:TextBox> 
           <asp:RequiredFieldValidator ID="UserNameRequired" runat="server" ControlToValidate="UserName" 
            CssClass="failureNotification" ErrorMessage="User Name is required." ToolTip="User Name is required." 
            ValidationGroup="RegisterUserValidationGroup">*</asp:RequiredFieldValidator> 
          </p> 
          <p> 
           <asp:Label ID="EmailLabel" runat="server" AssociatedControlID="Email">E-mail:</asp:Label> 
           <asp:TextBox ID="Email" runat="server" CssClass="textEntry"></asp:TextBox> 
           <asp:RequiredFieldValidator ID="EmailRequired" runat="server" ControlToValidate="Email" 
            CssClass="failureNotification" ErrorMessage="E-mail is required." ToolTip="E-mail is required." 
            ValidationGroup="RegisterUserValidationGroup">*</asp:RequiredFieldValidator> 
          </p> 
          <p> 
           <asp:Label ID="PasswordLabel" runat="server" AssociatedControlID="Password">Password:</asp:Label> 
           <asp:TextBox ID="Password" runat="server" CssClass="passwordEntry" TextMode="Password"></asp:TextBox> 
           <asp:RequiredFieldValidator ID="PasswordRequired" runat="server" ControlToValidate="Password" 
            CssClass="failureNotification" ErrorMessage="Password is required." ToolTip="Password is required." 
            ValidationGroup="RegisterUserValidationGroup">*</asp:RequiredFieldValidator> 
          </p> 
          <p> 
           <asp:Label ID="ConfirmPasswordLabel" runat="server" AssociatedControlID="ConfirmPassword">Confirm Password:</asp:Label> 
           <asp:TextBox ID="ConfirmPassword" runat="server" CssClass="passwordEntry" TextMode="Password"></asp:TextBox> 
           <asp:RequiredFieldValidator ControlToValidate="ConfirmPassword" CssClass="failureNotification" Display="Dynamic" 
            ErrorMessage="Confirm Password is required." ID="ConfirmPasswordRequired" runat="server" 
            ToolTip="Confirm Password is required." ValidationGroup="RegisterUserValidationGroup">*</asp:RequiredFieldValidator> 
           <asp:CompareValidator ID="PasswordCompare" runat="server" ControlToCompare="Password" ControlToValidate="ConfirmPassword" 
            CssClass="failureNotification" Display="Dynamic" ErrorMessage="The Password and Confirmation Password must match." 
            ValidationGroup="RegisterUserValidationGroup">*</asp:CompareValidator> 
          </p> 
         </fieldset> 
         <p class="submitButton"> 
          <asp:Button ID="CreateUserButton" runat="server" CommandName="MoveNext" Text="Create User" 
           ValidationGroup="RegisterUserValidationGroup"/> 
         </p> 
        </div> 
       </ContentTemplate> 
       <CustomNavigationTemplate> 
       </CustomNavigationTemplate> 
      </asp:CreateUserWizardStep> 

任何想法,我在做什麼錯誤? 謝謝,Laziale

+0

有很多的控制我假設你還可以找到控件您正在尋找for或foreach循環 – MethodMan 2011-12-29 17:55:02

回答

2

這是因爲您的UserName文本框不是頁面的直接子節點。它嵌套在許多控件中。以下是如何,遞歸,您可以通過所有的控件在你的頁面搜索,讓您UserName文本框的實例:該Web表單上

private TextBox getUserNameTextBox(ControlCollection ctls) 
    { 
     foreach (Control c in ctls) 
     { 
      if (c is System.Web.UI.WebControls.TextBox) 
      { 
       if (c.ID == "UserName") 
        return c; 
      } 
      if (c.HasControls()) 
      { 
       getAllCtl(c.Controls); 


      } 
     } 
     return null; 
    } 

YourUserNameTextBox = getuserNameTextBox(Page.Controls); 
+0

謝謝,這工作 – Laziale 2011-12-29 18:08:34

相關問題