2

I have an aspx page with an ajax tab container. In a class I want to find the tab container to pass some values.

I define myPage:

Page myPage = (Page)HttpContext.Current.Handler;

When looking in more details on this myPage by clicking add watch it is listing the tab container I am looking for. However when I define my tab container

AjaxControlToolkit.TabContainer Workflow_TabContainer = null;
Workflow_TabContainer = 
         (AjaxControlToolkit.TabContainer)myPage.FindControl("Workflow_TabContainer")
         as AjaxControlToolkit.TabContainer;

or

AjaxControlToolkit.TabContainer Workflow_TabContainer 
        (AjaxControlToolkit.TabContainer)myPage.FindControl("Workflow_TabContainer");

it does not find the tab container. I also tried to first define the page, than the ContentPlaceholder and searched for the tab container in the place holder. Same issue.

Any help and/or hint is much appreciated.

Thanks

Zhaph - Ben Duguid
  • 26,785
  • 5
  • 80
  • 117
Yann
  • 95
  • 2
  • 7
  • From the code you've provided, it doesn't seem like you add the `Workflow_TabContainer` to the page at all before trying to find. How is the `.FindControl` going to find a control that doesn't exist yet? –  Jul 04 '11 at 13:44
  • possible duplicate (in as much as Yann wishes to find a control on a page from a separate class - the control might be different, but the solution is the same) of [Using FindControl to get GridView in a Content Page](http://stackoverflow.com/questions/6525804/using-findcontrol-to-get-gridview-in-a-content-page) – Zhaph - Ben Duguid Jul 04 '11 at 13:47
  • @ziga. The tabcontainer is not added dynamically but is statically on the site. i only add the tabpanels dynamically. the container is definitely there, i can see it :) – Yann Jul 04 '11 at 13:57

2 Answers2

2

The FindControl method only looks in the current control for children.

If you don't know where in the page hierarchy the controls are, you'll need to do a recursive search - which is likely if you're using a templated control such as the TabContainer.

As I've posted previously to a similar answer:

private Control FindControlRecursive(Control rootControl, string controlID)
{
  if (rootControl.ID == controlID) {
    return rootControl;
  }

  foreach (Control controlToSearch in rootControl.Controls)
  {
    Control controlToReturn = 
      FindControlRecursive(controlToSearch, controlID);
    if (controlToReturn != null) { 
      return controlToReturn;
    }
  }

  return null;
}

Once you've got your control, you should cast it using as and then check for null just in case it's not quite what you were expecting:

var tabContainer = FindControlRecursively(myPage, "Workflow_TabContainer")
                 as AjaxControlToolkit.TabContainer

if (null != tabContainer) {
  // Do Stuff
}
Community
  • 1
  • 1
Zhaph - Ben Duguid
  • 26,785
  • 5
  • 80
  • 117
  • could you please explain a bit more detailed what exaclty a recursive search is.. i am new to asp.net and c# and don't lnow what is meant by recursive. do you maybe have an example? – Yann Jul 04 '11 at 13:47
  • Hi Yann - I added an example of some code you could use. You'll need to have a method like the `FindControlRecursive` method, which you'll notice calls itself (hence the term "recursive") and then you call that method with your `myPage` variable and the name of the control you want to find. The method will then look at each control in the `Controls` collection, if it's the one you want, it will return it, if it's a control, it will pass that control to the method until it either finds the one you want, or doesn't find anything, in which case you'll get a `null`. – Zhaph - Ben Duguid Jul 04 '11 at 13:59
  • i actually know where in the page hierachy the tab container is.. i am using a masterpage. and the container is in one placeholder of this masterpage. thats it.. on the aspx site i find the container without problems it is only in the class where i can't find it – Yann Jul 04 '11 at 14:02
  • 1
    Hi Zhaph, thanks so much for the help. it worked! and also thanks to all the other answers!! – Yann Jul 04 '11 at 14:20
  • @Yann There's a little Tick outline underneath each answer's vote counter - if an answer provides you with the solution, it's good form to check it, to let others know which answer worked (it also gives the answerer some additional reputation which is nice ;)) – Zhaph - Ben Duguid Jul 04 '11 at 15:02
0

if the control is on the same page you can directly access the control. Have a look at the below:

http://www.dotnetcurry.com/ShowArticle.aspx?ID=178

Massimiliano Peluso
  • 26,379
  • 6
  • 61
  • 70