views:

72

answers:

3

Hello,

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?

+2  A: 

FindControl only

Searches the current naming container for the specified server control.

i.e. It only checks the current containers direct children.

You can use the Controls property to return all the children of step:

ControlCollection children = step.Controls;

and enumerate over this looking for your text box.

ChrisF
@ChrisF: notice that the table is not the naming container for the TextBox, it's not a web control.
Claudio Redi
Thanks, I saw the documentation already, but don't understand what `current naming container` means. And worse, `TextBox nickName = FindControl("NickName") as TextBox;` didn't work either :(
phunehehe
@phunehehe - "current naming container" in this case means `step`. @Claudio has a point about the table not being a web control, but there's still a level of indirection.
ChrisF
+3  A: 

you may want to use a recursive find control function such as here: http://stevesmithblog.com/blog/recursive-findcontrol/

derek
thanks, the link explain a lot, but is it the only way to get that control of mine? It's kind of ridiculous to write new code myself just to get a simple control
phunehehe
@phunehehe - yes. this is the only way.
Sky Sanders
A: 

I don't know the control myself, but doesn't CreateUserWizardStep1.NickNameLabel work?

Doobi
sorry, it doesn't appear so
phunehehe