homepage Welcome to WebmasterWorld Guest from 54.161.240.10
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Home / Forums Index / Microsoft / Microsoft IIS Web Server and ASP.NET
Forum Library, Charter, Moderators: ocean10000

Microsoft IIS Web Server and ASP.NET Forum

    
Master Page Dynamic CSS changes from content page
Change css from a content page that is a child of a Master Page
mdsww




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

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.

 

Ocean10000




msg:4373971
 1:06 pm on Oct 13, 2011 (gmt 0)

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.

mdsww




msg:4374209
 11:02 pm on Oct 13, 2011 (gmt 0)

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.

mdsww




msg:4374223
 12:25 am on Oct 14, 2011 (gmt 0)

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?
marcel




msg:4374359
 9:10 am on Oct 14, 2011 (gmt 0)

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)

mdsww




msg:4374380
 11:01 am on Oct 14, 2011 (gmt 0)

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.

marcel




msg:4374796
 6:31 am on Oct 15, 2011 (gmt 0)

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.

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Microsoft / Microsoft IIS Web Server and ASP.NET
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved