2010-05-05 69 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

相關問題