shole
shole

Reputation: 4084

Get Masterpage's Telerik RadToolBar in content page code behind

I am now using Telerik AJAX to develop a ASP.NET web form.

There is a Telerik RadToolBar control in the master page:

...
<td width="100%">
  <div class="toolDiv">
    <telerik:RadToolBar ID="tbToolbar" runat="server" AutoPostBack="True" OnButtonClick="Toolbar_ButtonClick"
      OnClientButtonClicking="Toolbar_ClientButtonClicking" />
  </div>
</td>
...

For some reason I would like to get this toolbar control in one of the content page code behind using this master page, however I try

Master.FindControl("tbToolbar")

does not give me the toolbar control object, I also tried MainMasterPage.FindControl() and have no luck.

Is there a proper way to achieve what I want to do here? Thanks

EDITED:

My tbToolbar is located in Master Page as structure at below:

<asp:Content ID="content" ContentPlaceHolderID="myPlaceHolder" runat="server">  
    ...
    <table>
      <tr>
        <td>
          <table>
            <tr>
              <td>
                <div>
                    <telerik:RadToolBar ID="tbToolbar">
  
</asp:Content>

EDITED2: (Solution)

As suggested by the accepted answer, I add a class to recursively find my control, I post it here in case anyone found it useful:

private class ControlFinder<T> where T : Control
{
  private readonly List<T> _foundControls = new List<T>();
  public IEnumerable<T> FoundControls
  {
    get { return _foundControls; }
  }

  public void FindChildControlsRecursive(Control control)
  {
    foreach (Control childControl in control.Controls)
    {
      if (childControl.GetType() == typeof(T))
      {
        _foundControls.Add((T)childControl);
      }
      else
      {
        FindChildControlsRecursive(childControl);
      }
    }
  }
}

protected void Page_Load(object sender, EventArgs e)
{

  if (!IsPostBack)
  {
    ControlFinder<RadToolBar> controlFinder = new ControlFinder<RadToolBar>();
    controlFinder.FindChildControlsRecursive(Master);
    RadToolBar toolBar = controlFinder.FoundControls.FirstOrDefault();
    // my logic //
    if(toolBar != null) ...
  }

}

Upvotes: 0

Views: 298

Answers (1)

Sandeep
Sandeep

Reputation: 1210

If your control "tbToolbar" is inside ContentPlaceHolder then you have to first find the ContentPlaceHolder by id and then you have to find your control from that ContentPlaceHolder. Take a look at below link:

To reference a control on the master page

if control still can't be found then google for Find ASP.NET control recursively.

Upvotes: 2

Related Questions