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

Hamid Talebi picture Hamid Talebi · May 7, 2013 · Viewed 14.6k times · Source

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.

Answer

RMalke picture RMalke · May 7, 2013

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