如何在内容页中更改母版页的asp.net控件的可见性
本文关键字:母版页 asp net 可见性 控件 | 更新日期: 2023-09-27 18:12:30
我有一个Master Page
,有一个asp:Panel
控制和代码设置Visible = False
在它的代码后面。
现在我想改变Visible = True
在一个内容页。怎么做呢?
母版页代码:
AccountUserInfo.Visible = false;
内容页代码:
((Panel)Master.FindControl("AccountUserInfo")).Visible = true;
显然内容页后面的代码不工作
将控件设置为Visible = False
的母版页代码在该页上的代码之后执行。
尝试将页面代码放置在PreRender
事件上。这是周期的最后事件之一:
protected override void OnPreRender(EventArgs e)
{
((Panel)Master.FindControl("AccountUserInfo")).Visible = true;
base.OnPreRender(e);
}
还有,看一下这个ASP。. NET页面生命周期图
在ASP网站的活周期中,Page的代码先于母版的代码运行。
所以你基本上只是覆盖"可见"设置之前设置为"真"时,在你的主页你做:
还请注意,如果AccountUserInfo的任何父容器的可见性设置为false,则AccountUserInfo。可见getter将返回false (IMHO:微软在那里做了一个糟糕的选择…)。
试试这个
protected void Page_PreRender(object sender, EventArgs e)
{
((Panel)Master.FindControl("panel")).Visible = true;
}
希望对你有所帮助
试试这个
内容页
protected void Page_PreRender(object sender, EventArgs e)
{
Panel panel = (Panel)Master.FindControl("panel");
panel.Visible = true;
}
如果母版页与其控件之间的连接是直接的,那么前面的答案可能有效。在其他情况下,您可能想要查看您试图"查找"和更改的对象的层次结构。例如,如果从MasterPage调用FindControl可能会返回null(这取决于您的内容页面的结构,例如MasterPage> Menu> MenuItem> control)
因此,考虑到这一点,你可能想在内容页后面的代码中做这样的事情:protected void Page_PreRender(object sender, EventArgs e)
{
ParentObj1 = (ParentObj1)Master.FindControl("ParentObj1Id");
ParentObj2 = (ParentObj2)ParentObj1.FindControl("ParentObj1Id"); // or some other function that identifies children objects
...
Control ctrl = (Control)ParentObjN.FindControl("ParentObjNId"); // or some other function that identifies children objects
// we change the status of the object
ctrl.Visible = true;
}
或者我会给你一个实际的片段,为我工作(使按钮id ButtonViewReports隐藏在MasterPage,可见的内容页):
protected override void OnPreRender(EventArgs e)
{
// find RadMenu first
RadMenu rm = (RadMenu)this.Master.FindControl("MenuMaster");
if (rm != null)
{
// find that menu item inside RadMenu
RadMenuItem rmi = (RadMenuItem)rm.FindItemByValue("WorkspaceMenuItems");
if (rmi != null)
{
// find that button inside that Menitem
Button btn = (Button)rmi.FindControl("ButtonViewReports");
if (btn != null)
{
// make it visible
btn.Visible = true;
}
}
}
base.OnPreRender(e);
}