Question

I have a Master Page that have a asp:Panel control and the code that sets Visible = False in it's code behind.
Now i want to change Visible = True in one of content page. 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.

Était-ce utile?

La solution

The master page code that set the control to Visible = False is being executed after the code on the page.

Try to place the page code on the PreRender event. 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

Autres conseils

In the ASP website live cycle, Page's code runs before Master page's code does.

So you're basically just overwrite the "Visible" setting previously set to "true" when in your Master Page you do : 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...).

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. I.E. FindControl might return null if called from MasterPage (this depends on how your content page is structured, e.g. 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):

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);
}
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top