Welcome to WebmasterWorld Guest from 54.205.75.60

Forum Moderators: ocean10000

Message Too Old, No Replies

Master Page Dynamic CSS changes from content page

Change css from a content page that is a child of a Master Page

   
9:09 am on Oct 13, 2011 (gmt 0)

5+ Year Member



Hi,

I have multiple sites in my application, each identified uniquely with a SiteID. I have enabled URLRewriting to look up the content for each request dynamically.

The issue I am now faced with is how to hide or show DIVs on my Master Page dynamically from the content aspx page.

Currently I can update a label from my aspx page with the following:

AppMaster.master
<asp:Label ID="lblMenu" runat="server" Text="Test"></asp:Label>


AppMaster.master.cs
public string MasterPageMenuLabel
{
get { return lblMenu.Text; }
set { lblMenu.Text = value; }
}


Content.aspx:
<%@ MasterType VirtualPath="~/AppMasterTest.master" %>


Content.aspx.cs:
Master.MasterPageMenuLabel = intSiteID.ToString();


This works fine if I want to pass a simple string and update a label. (In my example I am populating a label with the SiteID number.)

What I would like to do is turn on or off a DIV's Visible property with an ID using a similar method (or better).

The SiteID will determine which menu to show or hide. Currently this is the only way I can think of, given my relative lack of experience with ASP.Net and c#. (I have primarily programmed in Classic ASP VBScript and PHP).

I was thinking of doing a select case on the SiteID, and then firing the appropriate command to show/hide the correct DIVs....
div1 (contains menu for Site1)
div2 (contains menu for Site2)
div3 (contains menu for Site3)....

Something like:
if SiteID = 1 then turn on div1


Can someone please help me fill in the blanks?
Thank you in advance.
1:06 pm on Oct 13, 2011 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



The way you described it is very similar how I have one of my sites except I use custom user controls instead of divs so I can further customize the displays.

But for simple menu's I think what you are doing will work fine and will be easy enough for you to manage.
11:02 pm on Oct 13, 2011 (gmt 0)

5+ Year Member



Hi Ocean,

How do I reference the div and set it's display css property from the Content.aspx.cs?

<div id="menu1">
...
</div>
<div id="menu2">
...
</div>
<div id="menu3">
...
</div>
<div id="menu4">
...
</div>


Thank you.
12:25 am on Oct 14, 2011 (gmt 0)

5+ Year Member



I got it working with the following:


if (intSiteID == 1)
{
this.Master.FindControl("menu1").Visible = true;
this.Master.FindControl("menu2").Visible = false;
this.Master.FindControl("menu3").Visible = false;
this.Master.FindControl("menu4").Visible = false;
}
else if (intSiteID == 2)
{
this.Master.FindControl("menu1").Visible = false;
this.Master.FindControl("menu2").Visible = true;
this.Master.FindControl("menu3").Visible = false;
this.Master.FindControl("menu4").Visible = false;
}
else if (intSiteID == 3)
{
this.Master.FindControl("menu1").Visible = false;
this.Master.FindControl("menu2").Visible = false;
this.Master.FindControl("menu3").Visible = true;
this.Master.FindControl("menu4").Visible = false;
}
else if (intSiteID == 4)
{
this.Master.FindControl("menu1").Visible = false;
this.Master.FindControl("menu2").Visible = false;
this.Master.FindControl("menu3").Visible = false;
this.Master.FindControl("menu4").Visible = true;
}


I want to do this in 2 lines but this is the only way I got it working. Also I am unsure whether
this.Master.FindControl
is a performance overhead?
9:10 am on Oct 14, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



To make your above code a little easier to maintain when changing the menus, you could condense it to the following:

this.Master.FindControl("menu1").Visible = (intSiteID == 1);
this.Master.FindControl("menu2").Visible = (intSiteID == 2);
this.Master.FindControl("menu3").Visible = (intSiteID == 3);
this.Master.FindControl("menu4").Visible = (intSiteID == 4);


I usually use placholders for this kind of thing, like this
<asp:PlaceHolder ID="plcMenu1" runat="server">
...
</asp:PlaceHolder>

and make it visible/non-visible like this:
plcMenu1.Visible = (intSiteID == 1)
11:01 am on Oct 14, 2011 (gmt 0)

5+ Year Member



Thank you, that is exactly what I was looking for!

I still am unclear if this is a performance overhead given it has iterate through the controls:
this.Master.FindControl("menu1")


...as I note that you do not reference the Master page in your final example.
6:31 am on Oct 15, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Oh sorry, I should have been more clear on that. Because the menu is on the MasterPage I usually show/hide the elements in CodeBehind of the MasterPage itself, and not from a subpage.

You could use a public property (eg. intSiteId) on the MasterPage and use that to display the Menu's. Just update that value from Content.aspx.cs

So basically:
MasterPage

private int _siteId;
public int SiteId
{
get { return _siteId; }
set { _siteId = value; }
}

and in Page_PreRender (I usually use Prerender for this and not PageLoad, as the Property SiteId may not yet have been set at the PageLoad point)

plcMenu1.Visible = (_siteId == 1)
plcMenu2.Visible = (_siteId == 2)
plcMenu3.Visible = (_siteId == 3)
plcMenu4.Visible = (_siteId == 4)


And then from Content.aspx.cs

Master.SiteId = intSiteId;


I'm not completely sure of your setup though, maybe the master page already knows the siteId, or maybe I've interpreted it completely wrong.

hope this helps though.