为什么我不能通过ID引用TextBox当它在CreateUserWizard控件中?

时间:2022-01-24 01:15:34

I got a wierd problem here.

我在这里遇到了一个奇怪的问题。

Inside an asp.net CreateUserWizard, I got some elements, but I can't seem to access them from my code-behind.

在asp.net CreateUserWizard中,我有一些元素,但我似乎无法从我的代码隐藏中访问它们。

Here's a code snippet:

这是一段代码片段:

Markup:

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="CreateUserWizard.ascx.cs" Inherits="Web.UserControls.CreateUserWizard" %>
<asp:CreateUserWizard ID="CreateUserWizard1" runat="server" OnCreatingUser="CreatingUser">
<WizardSteps>
    <asp:CreateUserWizardStep ID="CreateUserWizardStep1" runat="server">
        <ContentTemplate>
            <table border="0">
               <tr>
                    <td>
                        <span class="requiredField"/>
                        <asp:Label ID="NameLabel" runat="server" AssociatedControlID="NameRequiredFieldValidator">Navn:</asp:Label>                      
                        <asp:CheckBox ID="ShareInfoCheckBox" runat="server" Checked="True" Text="Share my information with partner sites." />
                    </td>
                    <td>
                        <asp:TextBox ID="Name" runat="server"></asp:TextBox>
                        <asp:RequiredFieldValidator ID="NameRequiredFieldValidator" runat="server" 
                            ControlToValidate="Name" ErrorMessage="Du skal indtaste dit navn" 
                            ToolTip="Du skal indtaste dit navn" ValidationGroup="CreateUserWizard1">*</asp:RequiredFieldValidator>
                    </td>
                </tr>
             </ContentTemplate>
    </asp:CreateUserWizardStep>
</WizardSteps>
</asp:CreateUserWizard>

.. And here's the codebehind:

..这是代码隐藏:

public partial class CreateUserWizard : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void CreatingUser(object sender, EventArgs e)
    {
        Name. //no intellisense and compiler error when I try to access Name
    }
}

Shouldn't this work? It's inside a UserControl if that makes any difference.

这不应该工作吗?如果这有任何区别,它在UserControl中。

Thanks in advance

提前致谢

4 个解决方案

#1


TextBox nameTextBox =
  CreateUserWizard1.CreateUserStep.ContentTemplateContainer.FindControl("Name") as TextBox;

if (nameTextBox != null) {
    /* Do your stuff */
}

More examples here.

这里有更多例子。

#2


Try this:

((TextBox)CreateUserWizardStep1.FindControl("Name")).Text = "Hello";

The reason for this is that your textbox is inside another object, so you cant access it directly.

原因是您的文本框位于另一个对象中,因此您无法直接访问它。

#3


Is there something else called 'Name' somewhere, a member variable, or a property? Try giving the control a less generic name like "Navn" or something less likely to conflict.

在某处还有一些名为'Name'的东西,一个成员变量或一个属性?尝试给控件一个不太通用的名称,如“Navn”或不太可能发生冲突的东西。

#4


Sean Bright has the right idea for how to get to it.

Sean Bright对如何实现它有正确的想法。

As far as "Why": this is a templated control, so the item you are trying to access exist inside of another naming container. You can use the Control.FindControl function to get access to the controls you need.

至于“为什么”:这是一个模板控件,因此您尝试访问的项目存在于另一个命名容器中。您可以使用Control.FindControl函数来访问所需的控件。

This is a good article that explains templated controls.

这是一篇很好的文章,解释了模板控件。

Or in the case of the other predefined fields for this wizard control, you can use CreateUserWizard1.UserName/Email/Password etc to access those values.

或者,对于此向导控件的其他预定义字段,您可以使用CreateUserWizard1.UserName / Email / Password等来访问这些值。

#1


TextBox nameTextBox =
  CreateUserWizard1.CreateUserStep.ContentTemplateContainer.FindControl("Name") as TextBox;

if (nameTextBox != null) {
    /* Do your stuff */
}

More examples here.

这里有更多例子。

#2


Try this:

((TextBox)CreateUserWizardStep1.FindControl("Name")).Text = "Hello";

The reason for this is that your textbox is inside another object, so you cant access it directly.

原因是您的文本框位于另一个对象中,因此您无法直接访问它。

#3


Is there something else called 'Name' somewhere, a member variable, or a property? Try giving the control a less generic name like "Navn" or something less likely to conflict.

在某处还有一些名为'Name'的东西,一个成员变量或一个属性?尝试给控件一个不太通用的名称,如“Navn”或不太可能发生冲突的东西。

#4


Sean Bright has the right idea for how to get to it.

Sean Bright对如何实现它有正确的想法。

As far as "Why": this is a templated control, so the item you are trying to access exist inside of another naming container. You can use the Control.FindControl function to get access to the controls you need.

至于“为什么”:这是一个模板控件,因此您尝试访问的项目存在于另一个命名容器中。您可以使用Control.FindControl函数来访问所需的控件。

This is a good article that explains templated controls.

这是一篇很好的文章,解释了模板控件。

Or in the case of the other predefined fields for this wizard control, you can use CreateUserWizard1.UserName/Email/Password etc to access those values.

或者,对于此向导控件的其他预定义字段,您可以使用CreateUserWizard1.UserName / Email / Password等来访问这些值。