I have implemented master pages using this example How to implement a status bar in an ASP.NET application?. I have a property on my SiteMaster.cs inherited MasterPage called Environment. On my MasterPage.master I have this code:
<body>
<form id="frmMaster" runat="server">
<.. some content removed for brevity ...>
Environment: <%= this.Environment %>
</form>
</body>
What I would like to do is evaluate this.Environment and if it is “LIVE” then colour the background of this.Environment text red, and if it’s “TEST” colour it yellow. How would I do this?
UPDATE I’ve just added this code to MasterPage.master
protected void Page_Load(object sender, EventArgs e)
{
lblEnvironment.Text = this.Environment;
if (this.Environment == "LIVE")
{
lblEnvironment.BackColor = System.Drawing.Color.Red;
}
}
The page loads, but the text does not get set, it’s blank! Also the old text, that was populated is now blank too (I left the old code there for now). I also get a warning in Visual Studio:
‘ASP.masterpage_master.Page_Load(object,
System.EventArgs)’ hides inherited
member ‘SiteMaster.Page_Load(object,
System.EventArgs)’. Use the new
keyword if hiding was intended.
UPDATE2: This is what I have in SiteMaster.cs
using System;
using System.Web.UI;
public class SiteMaster : MasterPage
{
public string StatusText { get; set; }
public string StatusTime { get; set; }
public string Environment { get; set; }
protected virtual void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
if (Session["status"] != null)
{
this.StatusText = Session["status"].ToString();
this.StatusTime = Session["statusTime"].ToString();
}
this.Environment = Session["environment"].ToString();
}
}
}
Instead of using the
<%=syntax to print out the environment (this is usingResponse.Write), consider using a server control like aLiteralor aLabel. As you want to change the background colour, this suggests styling (CSS), so aLabelwould be appropriate.(A
Literalis just a text placeholder and renders no HTML, whereas aLabel(usually) renders the text inside<span>tags.)So I would change your master page markup to
In the code-behind, set the
Textproperty ofenvironmentLabeltothis.Environment. At the same time, test the value of the evironment, and set theBackColorproperty of the label as appropriate (or apply a CSS class).UPDATE:
For a master page, you just need one class, which will inherit from
System.Web.UI.MasterPage. If you create this in Visual Studio and call itSiteMaster, you’ll get 3 files:SiteMaster.Master (the markup)
SiteMaster.Master.cs (the code-behind)
SiteMaster.Master.designer.cs (automatically generated/updated)
In the SiteMaster.Master file, you’ll want something like this:
In SiteMaster.Master.cs, you’ll need something like this:
As the environment label is on the master page, any normal page (ASPX) using this master page will get the label displayed. Every time a page is loaded, the
Page_Loadevent in SiteMaster.Master.cs will be called, and the text will be updated. You don’t need to define theMasterPageclass yourself, that’s provided by the .NET framework.You may want to improve this
Page_Loadmethod, either by using ViewState and therefore only setting the text if you’re not doing a PostBack, or by disabling ViewState on theenvironmentLabelcontrol.Finally, you’ll have one or more ASPX pages in your site, with something like this at the top of the markup: