1

I'm new at ASP.NET but something that continuously gives me trouble is finding nested server controls, especially when they are nested.

In this case, here is my registration page up until the server control I want:

<asp:CreateUserWizard runat="server" ID="RegisterUser" ViewStateMode="Disabled" OnCreatedUser="RegisterUser_CreatedUser">
    <LayoutTemplate>
        <asp:PlaceHolder runat="server" ID="wizardStepPlaceholder" />
        <asp:PlaceHolder runat="server" ID="navigationPlaceholder" />
    </LayoutTemplate>
    <WizardSteps>
        <asp:CreateUserWizardStep runat="server" ID="RegisterUserWizardStep">
            <ContentTemplate>
                <fieldset>
                    <ol>
                        <li>
                            <asp:TextBox runat="server" ID="firstName" />
                        </li>

For readability sake, the only things I've removed are some HTML elements. I am trying to access'firstName'. I've tried all of the following with no luck, (TextBox first is always coming up null).

TextBox first = (TextBox)Page.Master.FindControl("MainContent").FindControl("firstName");
TextBox first = (TextBox)Page.FindControl("firstName");
TextBox first = (TextBox)RegisterUserWizardStep.FindControl("firstName");
TextBox first = (TextBox)RegisterUser.FindControl("firstName");

Would appreciate help, thanks!

Katherine Perotta
  • 125
  • 1
  • 5
  • 15

2 Answers2

2

Often times you may need to do a recursive control search. First, add this method to your page:

private Control FindControlRecursive(Control Root, string Id)
{
    if (Root.ID == Id)
        return Root;
    foreach (Control Ctl in Root.Controls)
    {
        Control FoundCtl = FindControlRecursive(Ctl, Id);
        if (FoundCtl != null)
            return FoundCtl;
    }
    return null;
}

Now, to find the control, call:

TextBox firstName = (TextBox)FindControlRecursive(this, "firstName");
Adam Plocher
  • 13,994
  • 6
  • 46
  • 79
  • similar answer http://stackoverflow.com/questions/6572255/find-control-in-class-cannot-find-control. Try to give a link rather than posting it.. – Pradip Sep 30 '12 at 06:39
  • Why would I want to give a link when I can just give an answer? – Adam Plocher Sep 30 '12 at 06:45
  • Because that would keep the information centralised. whole point of Stackoverflow.. Nyways.. this the answer I was looking for.. THanks Mate – Pradip Sep 30 '12 at 06:46
  • Thank you, is there anywhere I can add this function so I can use it anywhere in my website instead of on a page basis? – Katherine Perotta Sep 30 '12 at 06:47
  • Do you have a BasePage that all pages inherit from? You can just change it from a private to a public method and put it in a basepage. Otherwise, you could change it to a public static method that gets called from a separate class (Class1.FindControlRecursive(...)). If this is a WebSite, the class should go in the AppCode folder. If it's a WebApp, it can go anywhere in your web project. – Adam Plocher Sep 30 '12 at 06:49
1

Try this piece of code:

TextBox first = (TextBox) RegisterUser.CreateUserStep.ContentTemplateContainer.FindControl("firstName");
Furqan Safdar
  • 16,260
  • 13
  • 59
  • 93
  • SUCCESS! Thank you! Do you mind explaining a bit of how you arrived at that? This is a recurring problem I have in my pages. – Katherine Perotta Sep 30 '12 at 06:39
  • There are different ways to find nested control. For CreateUserWizard, this is way to access, http://msdn.microsoft.com/en-us/library/ms178342%28v=vs.100%29.aspx – Furqan Safdar Sep 30 '12 at 06:42
  • An UpVote with acceptance of this answer would be highly encouraging if it really helped you. Thanks – Furqan Safdar Sep 30 '12 at 06:44