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

5

6

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.

Token answered 7/5, 2013 at 11:29 Comment(2)
Do you get an error of some sort? Are you sure that line get called?Lengthen
@Serge,No,every thing is good and i haven't any error!But it not worked!Token
N
8

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

Naturalism answered 7/5, 2013 at 11:41 Comment(0)
L
3

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...).

Lengthen answered 7/5, 2013 at 11:40 Comment(1)
Look at Page Life Cycle. You can also put debug breakpoints in your master page and in your content page, so you can see which one executes first.Mannerless
A
1

Try This one

protected void Page_PreRender(object sender, EventArgs e)
{

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

}

Hope It helps you

Airframe answered 7/5, 2013 at 11:45 Comment(0)
U
0

Try this

in content Page

protected void Page_PreRender(object sender, EventArgs e)
{

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

}
Urbai answered 7/5, 2013 at 11:43 Comment(0)
P
0

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);
}
Priceless answered 29/12, 2015 at 18:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.