如何找到控件?

时间:2011-04-16 18:03:51

标签: asp.net

<asp:LoginView ID="LoginView4" runat="server">
      <AnonymousTemplate>

      <asp:DropShadowExtender ID="DropShadowExtender3" runat="server" 
                                TargetControlID="Panel3" 
                                Opacity=".38" 
                                Rounded="true">
              </asp:DropShadowExtender>
              <asp:Panel ID="Panel3" runat="server" 
                                     BackColor="Silver" Width="400px">

              <asp:CreateUserWizard ID="CreateUserWizard1" 
                                    runat="server" 
                                    ContinueDestinationPageUrl="~/ViewCart_aspx/ViewCart.aspx" 
                      oncreateduser="CreateUserWizard1_CreatedUser">


              <WizardSteps>
                  <asp:CreateUserWizardStep ID="CreateUserWizardStep1" runat="server" >
                      <ContentTemplate>
                         <fieldset class="push">
                          <table border="0">
                              <tr>
                                  <td align="center" colspan="2">
                                      <h3 style="text-align:center;">Sign Up for Your New Account</h3></td>
                              </tr>
                              <tr>
                                  <td align="right">
                                      <asp:Label ID="UserNameLabel" runat="server" AssociatedControlID="UserName">User Name:</asp:Label>
                                  </td>
                                  <td>&nbsp;
                                      **<asp:TextBox ID="UserName" runat="server" Width="150px"></asp:TextBox>**
                                      <asp:RequiredFieldValidator ID="UserNameRequired" runat="server" 
                                          ControlToValidate="UserName" ErrorMessage="User Name is required." 
                                          ToolTip="User Name is required." ValidationGroup="CreateUserWizard1">*</asp:RequiredFieldValidator>
                                  </td>

我正在使用此代码来查找控件

Protected Sub CreateUserWizard1_CreatedUser(ByVal sender As Object, ByVal e As System.EventArgs)
    Dim UserNameTextBox As TextBox = DirectCast(CreateUserWizardStep1.ContentTemplateContainer.FindControl("UserName"), TextBox)
    Roles.AddUserToRole(UserNameTextBox.Text, "User")

End Sub

但它给了我一个错误

  

“错误12名称'CreateUserWizardStep1'未声明。”

1 个答案:

答案 0 :(得分:0)

您必须先在LoginView上使用FindControl()来引用CreateUserWizardStep1

Dim CreateUserWizardStep1 As CreateUserWizard = 
    DirectCast(LoginView4.FindControl("CreateUserWizard1"), CreateUserWizard)

然后你可以做

If CreateUserWizardStep1 IsNot Nothing 

     Dim UserNameTextBox As TextBox = DirectCast(CreateUserWizardStep1.ContentTemplateContainer.FindControl("UserName"), TextBox)
        Roles.AddUserToRole(UserNameTextBox.Text, "User")
End If