简体   繁体   English

在内容页面后面的代码中获取Masterpage的Telerik RadToolBar

[英]Get Masterpage's Telerik RadToolBar in content page code behind

I am now using Telerik AJAX to develop a ASP.NET web form. 我现在正在使用Telerik AJAX开发ASP.NET Web表单。

There is a Telerik RadToolBar control in the master page: 主页面中有一个Telerik RadToolBar控件:

 ... <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. 没有给我工具栏控件对象,我也尝试了MainMasterPage.FindControl()并且没有运气。

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: 我的tbToolbar位于母版页中,其结构如下:

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

EDITED2: (Solution) 编辑2 :(解决方案)

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) ... } } 

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. 如果控件“ tbToolbar”位于ContentPlaceHolder内,则必须首先通过ID查找ContentPlaceHolder,然后必须从该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. 如果仍然找不到控件,则Google递归地查找Find ASP.NET控件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM