views:

290

answers:

4

Hi all.. 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.

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.

Thanks in advance

A: 

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.

Aric TenEyck
+3  A: 

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.

Sergio
+6  A: 
TextBox nameTextBox =
  CreateUserWizard1.CreateUserStep.ContentTemplateContainer.FindControl("Name") as TextBox;

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

More examples here.

Sean Bright
I prefer the "as TextBox" notation it's less likely to throw an exception
Omar Kooheji
A: 

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

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.

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.

awl