我現在使用Telerik AJAX開發ASP.NET Web表單。獲取Masterpage的Telerik RadToolBar內容頁面代碼
有一個Telerik的RadToolBar控制在母版頁:
...
<td width="100%">
<div class="toolDiv">
<telerik:RadToolBar ID="tbToolbar" runat="server" AutoPostBack="True" OnButtonClick="Toolbar_ButtonClick"
OnClientButtonClicking="Toolbar_ClientButtonClicking" />
</div>
</td>
...
出於某種原因,我想在內容頁代碼一個拿到後面使用該主此工具欄控制頁面,但我嘗試
Master.FindControl("tbToolbar")
不GI把我的工具欄控制對象,我也試過MainMasterPage.FindControl()
,並沒有運氣。
有沒有一種合適的方式來實現我想在這裏做的事情?由於
編輯:
我tbToolbar位於母版頁的結構如下位置:
<asp:Content ID="content" ContentPlaceHolderID="myPlaceHolder" runat="server">
...
<table>
<tr>
<td>
<table>
<tr>
<td>
<div>
<telerik:RadToolBar ID="tbToolbar">
</asp:Content>
EDITED2:(解決方案)
由於由接口建議PTED答案,我添加一個類遞歸找到我的控制,我在這裏發佈的情況下,沒有人發現它是有用的:
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) ...
}
}
圍繞'tbToolbar'還有其他服務器端控件,如面板或類似的嗎? – Andrei