你可以在Page指令页设置启用Trace单页面跟踪,或者在Web.config中再启用全局跟踪,就可以看到Control Tree了。
采用MasterPage后的页面结构如下:
//Page --ASP.about_aspx
// Master Page --ASP.site_master
// (Master page markup and controls)
// ContentPlaceHolder --HeadContent
// Content page markup and server controls
// (Master page markup and controls)
// ContentPlaceHolder --MainContent
// Content page markup and server controls
// (Master page markup and controls)
无论我们用FindControl查找控件,或者是添加控件,一定是添加到ContenPlaceHolder内部的!// Master Page --ASP.site_master
// (Master page markup and controls)
// ContentPlaceHolder --HeadContent
// Content page markup and server controls
// (Master page markup and controls)
// ContentPlaceHolder --MainContent
// Content page markup and server controls
// (Master page markup and controls)
下面代码实现了动态添加控件以及两种查找控件的方法。
//BMK FindControlRecursive
/// <summary>
/// 迭代遍历控件,查找子控件
/// </summary>
/// <param name="rootControl">包含控件的容器</param>
/// <param name="controlIDToBeSEEK">要查找的控件的ID</param>
/// <returns></returns>
private Control FindControlRecursive(Control rootControl, string controlIDToBeSEEK)
{
if (rootControl.ID == controlIDToBeSEEK)
return rootControl;
foreach (Control control in rootControl.Controls)
{
Control controlToReturn = FindControlRecursive(control, controlIDToBeSEEK);
if (controlToReturn != null)
return controlToReturn;
}
return null;
}
//再有Masterpage 的时候,控件必须加到ContentPlaceHolder内
//要查找控件,也必须ContentPlaceHolder内FindControl,无论层次怎样,总能得到有效结果
protected void Button1_Click(object sender, EventArgs e)
{
for (int i = 0; i < 5; i++)
{
TextBox txtDynamicAdded = new TextBox();
txtDynamicAdded.ID = "txt" + i.ToString();
txtDynamicAdded.Text = "Hello,World!";
//可视状态,否则PostBack后动态添加的控件会不见
ViewState[txtDynamicAdded.ID] = true;
this.Master.FindControl("MainContent").Controls.Add(txtDynamicAdded);
}
Control ctrlFindbutton = FindControlRecursive(this, "Button1");
if (ctrlFindbutton != null)
{
Button btnFind = (Button)ctrlFindbutton;
btnFind.Text = "find" + DateTime.Now.ToString("T");
}
//如何才能访问MainContent呢
Control cc = this.Master.FindControl("MainContent").FindControl("Button1");
//Page --ASP.about_aspx
// Master Page --ASP.site_master
// (Master page markup and controls)
// ContentPlaceHolder --HeadContent
// Content page markup and server controls
// (Master page markup and controls)
// ContentPlaceHolder --MainContent
// Content page markup and server controls
// (Master page markup and controls)
}
/// <summary>
/// 迭代遍历控件,查找子控件
/// </summary>
/// <param name="rootControl">包含控件的容器</param>
/// <param name="controlIDToBeSEEK">要查找的控件的ID</param>
/// <returns></returns>
private Control FindControlRecursive(Control rootControl, string controlIDToBeSEEK)
{
if (rootControl.ID == controlIDToBeSEEK)
return rootControl;
foreach (Control control in rootControl.Controls)
{
Control controlToReturn = FindControlRecursive(control, controlIDToBeSEEK);
if (controlToReturn != null)
return controlToReturn;
}
return null;
}
//再有Masterpage 的时候,控件必须加到ContentPlaceHolder内
//要查找控件,也必须ContentPlaceHolder内FindControl,无论层次怎样,总能得到有效结果
protected void Button1_Click(object sender, EventArgs e)
{
for (int i = 0; i < 5; i++)
{
TextBox txtDynamicAdded = new TextBox();
txtDynamicAdded.ID = "txt" + i.ToString();
txtDynamicAdded.Text = "Hello,World!";
//可视状态,否则PostBack后动态添加的控件会不见
ViewState[txtDynamicAdded.ID] = true;
this.Master.FindControl("MainContent").Controls.Add(txtDynamicAdded);
}
Control ctrlFindbutton = FindControlRecursive(this, "Button1");
if (ctrlFindbutton != null)
{
Button btnFind = (Button)ctrlFindbutton;
btnFind.Text = "find" + DateTime.Now.ToString("T");
}
//如何才能访问MainContent呢
Control cc = this.Master.FindControl("MainContent").FindControl("Button1");
//Page --ASP.about_aspx
// Master Page --ASP.site_master
// (Master page markup and controls)
// ContentPlaceHolder --HeadContent
// Content page markup and server controls
// (Master page markup and controls)
// ContentPlaceHolder --MainContent
// Content page markup and server controls
// (Master page markup and controls)
}