为什么 ASP.NET 找不到我的文本框?
我正在尝试向 CreateUserWizardStep
添加更多字段,这是我添加的内容:
<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("")
?
I'm trying to add more fields to the CreateUserWizardStep
, here is what I added:
<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>
Then I tried to reference the objects like this
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
}
The problem is, I'm getting nulls for nickName
. Am I using FindControl("")
incorrectly?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可能想使用递归查找控制功能,如下所示: http://stevesmithblog.com/blog /递归查找控制/
you may want to use a recursive find control function such as here: http://stevesmithblog.com/blog/recursive-findcontrol/
FindControl
仅即它只检查当前容器的直接子容器。
您可以使用
Controls
属性返回step
的所有子级:并枚举此查找文本框。
FindControl
onlyi.e. It only checks the current containers direct children.
You can use the
Controls
property to return all the children ofstep
:and enumerate over this looking for your text box.
我自己不知道该控件,但是 CreateUserWizardStep1.NickNameLabel 不起作用吗?
I don't know the control myself, but doesn't CreateUserWizardStep1.NickNameLabel work?