2010-05-05 68 views
2

我想更多的字段添加到CreateUserWizardStep,这里是我补充说:为什么不能asp.net找到我的文本框?

<asp:CreateUserWizardStep ID="CreateUserWizardStep1" runat="server"> 
    <ContentTemplate> 
     <table border="0"> 
      <tr> 
       <td align="right"> 
        <asp:Label ID="NickNameLabel" runat="server" AssociatedControlID="NickName">Nick Name:</asp:Label> 
       </td> 
       <td> 
        <asp:TextBox ID="NickName" runat="server"></asp:TextBox> 
        <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="NickName" 
         ErrorMessage="Nick Name is required." ToolTip="Nick Name is required." ValidationGroup="CreateUserWizard1">*</asp:RequiredFieldValidator> 
       </td> 
      </tr> 
      <%-- The default code is left unchanged, but not shown here --%> 
     </table> 
    </ContentTemplate> 
</asp:CreateUserWizardStep> 

然后我试图引用的对象这样

protected void NewUserWizard_CreatedUser(object sender, EventArgs e) 
{ 
    CreateUserWizardStep step = NewUserWizard.FindControl("CreateUserWizardStep1") as CreateUserWizardStep; 
    TextBox nickName = step.FindControl("NickName") as TextBox; 
    // insert additional information to the database 
} 

的问题是,我获得空值为nickName。我错误地使用了FindControl("")吗?

+0

为什么不直接把它们称为this.NickName或this.CreateWizardSTep1? – n8wrl 2010-05-05 12:41:07

回答

3

您可能需要使用一个递归查找控制功能,比如在这里:http://stevesmithblog.com/blog/recursive-findcontrol/

+0

感谢,链接解释很多,但它让我那个控制的唯一方式吗?这是一种荒谬的编写新的代码,自己只是为了得到一个简单的控制 – phunehehe 2010-05-05 12:50:49

+0

@phunehehe - 。是的,这是唯一的方法 – 2010-05-05 12:57:43

2

FindControl

搜索指定的服务器控制当前命名容器。

即只检查当前容器直接孩子。

可以使用Controls属性返回的step所有的孩子:

ControlCollection children = step.Controls; 

,并列举在此寻找您的文本框中。

+0

@ChrisF:注意到表格是不是对TextBox命名容器,它不是一个网络控制。 – 2010-05-05 12:40:12

+0

谢谢,我看到的文件了,但不明白'当前的命名container'手段。更糟糕的是,'文本框昵称=的FindControl(“昵称”)的文本框;'没工作,要么:( – phunehehe 2010-05-05 12:42:30

+0

@phunehehe - 在这种情况下,“当前的命名容器”是指'step' @Claudio有一个关于该表不点作为一个网络控制,但还是有间接的级别。 – ChrisF 2010-05-05 12:44:27

0

我不知道控制自己,但不CreateUserWizardStep1.NickNameLabel工作?

+0

对不起,它不会出现这样 – phunehehe 2010-05-05 14:42:26

相关问题