简体   繁体   English

如何在内容页面中更改主页的asp.net控件的可见性?

[英]how to change visible of an asp.net control of master page in content page?

I have a Master Page that have a asp:Panel control and the code that sets Visible = False in it's code behind. 我有一个拥有asp:Panel控件的Master Page ,以及在其后面的代码中设置Visible = False的代码。
Now i want to change Visible = True in one of content page. 现在我想在其中一个内容页面中更改Visible = True How do it? 怎么样?

Master page code behind: 主页面代码背后:

AccountUserInfo.Visible = false;  

Content page code behind: 内容页面代码背后:

((Panel)Master.FindControl("AccountUserInfo")).Visible = true;

Apparently content page's code behind don't work. 显然内容页面的代码背后不起作用。

The master page code that set the control to Visible = False is being executed after the code on the page. 将控件设置为Visible = False的母版页代码正在页面上的代码之后执行。

Try to place the page code on the PreRender event. 尝试将页面代码放在PreRender事件上。 It is the one of the last events of the cycle: 这是周期的最后一个事件:

protected override void OnPreRender(EventArgs e)
{
    ((Panel)Master.FindControl("AccountUserInfo")).Visible = true; 
    base.OnPreRender(e);
}

Also, take a look at this ASP.NET Page Life Cycle Diagram 另外,请看一下这个ASP.NET页面生命周期图

In the ASP website live cycle, Page's code runs before Master page's code does. 在ASP网站的实时周期中,Page的代码在Master页面的代码之前运行。

So you're basically just overwrite the "Visible" setting previously set to "true" when in your Master Page you do : AccountUserInfo.Visible=false; 所以你基本上只是在主页面中覆盖之前设置为“true”的“可见”设置:AccountUserInfo.Visible = false;

Also note that if any parent container of AccountUserInfo has a visibility set to false, AccountUserInfo.Visible getter will return false (IMHO: a poor choice Microsoft made there...). 另请注意,如果AccountUserInfo的任何父容器的可见性设置为false,则AccountUserInfo.Visible getter将返回false(恕我直言:微软在那里做出的糟糕选择......)。

Try This one 试试这个

protected void Page_PreRender(object sender, EventArgs e)
{

((Panel)Master.FindControl("panel")).Visible = true;

}

Hope It helps you 希望它能帮到你

Try this 尝试这个

in content Page 在内容页面

protected void Page_PreRender(object sender, EventArgs e)
{

    Panel panel = (Panel)Master.FindControl("panel");
    panel.Visible = true;

}

Previous answers might work if the connection between the master page and its control is direct. 如果母版页与其控件之间的连接是直接的,则以前的答案可能有效。 In other cases you might want to take a look at the hierarchy of the object you're trying to 'find' and change. 在其他情况下,您可能想要查看您尝试“查找”和更改的对象的层次结构。 IE FindControl might return null if called from MasterPage (this depends on how your content page is structured, eg MasterPage > Menu > MenuItem > control) 如果从MasterPage调用IE FindControl可能会返回null(这取决于您的内容页面的结构,例如MasterPage> Menu> MenuItem> control)

so with that in mind you might want to do something like this at the content page code behind: 所以考虑到这一点,你可能想在后面的内容页面代码中做这样的事情:

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;
}

or I'll give you an actual snippet that worked for me (make button with id ButtonViewReports hidden at MasterPage, visible at content page): 或者我会给你一个适合我的实际代码片段(在主页面上隐藏了ID为ButtonViewReports的按钮,在内容页面上可见):

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

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

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