Bramble
Bramble

Reputation: 1395

How to get a control reference from .ascx on a .aspx page?

I have a wizard setup like so:

<asp:Wizard ID="Wizard1" runat="server" DisplaySideBar="false" onnextbuttonclick="OnNextButtonClick">
       <WizardSteps>
           <asp:WizardStep ID="WizardStep1" runat="server" Title="Step 1">
            <div class="content">
                <asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder>
            </div>
           </asp:WizardStep>
           <asp:WizardStep ID="WizardStep2" runat="server" Title="Step 2">
               <div class="content">
                   <asp:PlaceHolder ID="PlaceHolder2" runat="server"></asp:PlaceHolder>
               </div>
           </asp:WizardStep> ...more

and then on the prerender of that page I add the controls like so:

protected void Wizard1_PreRender(object sender, EventArgs e)
{
    PlaceHolder1.Controls.Add(LoadControl("Control1.ascx"));
    PlaceHolder2.Controls.Add(LoadControl("Control2.ascx"));
    PlaceHolder3.Controls.Add(LoadControl("Control3.ascx"));
    ...more controls added
 }

and on the .ascx control I have a ajaxToolkit:TabContainer that I want to access like this:

<ajaxToolkit:TabContainer ID="TabContainer1" runat="server" ActiveTabIndex="0">  
            <ajaxToolkit:TabPanel ID="TabPanel1" HeaderText="PRV 1" runat="server">

So what I want to do is stop the wizard from going to the next step and move the ajaxtoolkit:Tabcontainer to the next tab. I am accessing the nextbutton like so:

protected void OnNextButtonClick(object sender, WizardNavigationEventArgs e)
{
     if(Wizard1.ActiveStepIndex == 2)
     {
          e.Cancel = true;
          //get ajaxToolKit:tabcontrol here
     }
}

Any ideas on how to access the TabControl in the OnNextButtonClick function? This is a asp.net webapplication.

Upvotes: 1

Views: 3037

Answers (1)

User.Anonymous
User.Anonymous

Reputation: 1726

You can use Page.GetControl("TabContainer1") or use a recursive method if you don't know the depth from your Page (useful with .ascx).

public static Control FindControlRecursive(Control container, string name)
{
    if ((container.ID != null) && (container.ID.Equals(name)))
        return container;

    foreach (Control ctrl in container.Controls)
    {
        Control foundCtrl = FindControlRecursive(ctrl, name);
        if (foundCtrl != null)
            return foundCtrl;
    }
    return null;
}

Use it FindControlRecursive(Page, "TabContainer1")

Upvotes: 2

Related Questions