2010-11-18 139 views
0

我已经使用此示例How to implement a status bar in an ASP.NET application?实现了母版页。我的SiteMaster.cs上有一个属性,它继承了名为Environment的MasterPage。在我MasterPage.master我有这样的代码:以编程方式更改某些文本的背景颜色

<body> 
    <form id="frmMaster" runat="server"> 
     <.. some content removed for brevity ...> 

     Environment: <%= this.Environment %> 
    </form> 
</body> 

我想要做的是评估this.Environment,如果它是“活”然后选择颜色this.Environment文字的背景红色的,如果它的“TEST”为它着黄色。我将如何做到这一点?

UPDATE我刚加入这个代码MasterPage.master

protected void Page_Load(object sender, EventArgs e) 
{ 
    lblEnvironment.Text = this.Environment; 
    if (this.Environment == "LIVE") 
    { 
     lblEnvironment.BackColor = System.Drawing.Color.Red; 
    }     
} 

该页面加载,但文字不被设置,它是空白!此外,填充的旧文本现在也是空白的(我现在留下了旧代码)。我也得在Visual Studio中警告:

'ASP.masterpage_master.Page_Load(对象, System.EventArgs)' 隐藏继承 成员 'SiteMaster.Page_Load(对象, System.EventArgs)'。如果需要隐藏,请使用新的 关键字。

UPDATE2:这是我在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(); 
     } 

    } 
} 

回答

3

除了使用<%=语法打印出环境(这是使用Response.Write)的,可以考虑使用一个服务器控件一样一个Literal或一个Label。由于您想更改背景颜色,因此建议使用样式(CSS),因此Label将是合适的。

(A Literal只是一个文本占位符,并呈现无HTML,而Label(通常)呈现内<span>标签的文本。)

所以我会改变你的母版页标记来

Environment: <asp:Label ID="environmentLabel" runat="server" /> 

在代码隐藏中,将environmentLabelText属性设置为this.Environment。同时,测试环境的值,并根据需要设置标签的BackColor属性(或应用CSS类)。

UPDATE:
对于一个母版页,你只需要一个类,它将从System.Web.UI.MasterPage继承。如果您在Visual Studio中创建此并调用它SiteMaster,你会得到3个文件:

SiteMaster.Master(标记)
SiteMaster.Master.cs(代码隐藏)
SiteMaster.Master.designer的.cs(自动生成/更新)

在SiteMaster.Master文件,你会想是这样的:

<%@ Master Language="C#" AutoEventWireup="true" CodeBehind="SiteMaster.master.cs" Inherits="WebApplication1.SiteMaster" %> 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 

<html xmlns="http://www.w3.org/1999/xhtml"> 
<head runat="server"> 
    <title></title> 
    <asp:ContentPlaceHolder ID="head" runat="server"> 
    </asp:ContentPlaceHolder> 
</head> 
<body> 
    <form id="form1" runat="server"> 
    <div> 
     <asp:Label ID="environmentLabel" runat="server" /> 

     <asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server" /> 
    </div> 
    </form> 
</body> 
</html> 

在SiteMaster.Master。CS,你需要这样的事:

using System; 

namespace WebApplication1 
{ 
    public partial class SiteMaster : System.Web.UI.MasterPage 
    { 
     protected void Page_Load(object sender, EventArgs e) 
     { 
      this.environmentLabel.Text = "environment"; 
      this.environmentLabel.BackColor = System.Drawing.Color.Red; 
     } 
    } 
} 

随着环境的标签是母版页上,任何正常的页面(ASPX)使用该母版页将得到显示的标签。每次加载页面时,SiteMaster.Master.cs中的Page_Load事件都将被调用,并且文本将被更新。您不需要自己定义MasterPage类,这是由.NET框架提供的。

您可能想要改进Page_Load方法,或者通过使用ViewState,因此只在不执行PostBack时设置文本,或者通过禁用environmentLabel控件上的ViewState。

最后,你必须在你的网站上的一个或多个ASPX页面,像这样的东西在标记的顶部:

<%@ Page Title="" Language="C#" MasterPageFile="~/SiteMaster.Master" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication1.WebForm1" %> 
+1

我应该在哪个事件中设置BackColor?问题是'MasterPage'继承自SiteMaster.cs,并且SiteMaster.cs上有一个'Page_Load'方法。我不能在MasterPage.master和SiteMaster.cs中有一个'Page_Load'方法吗?这是我卡住的地方,谢谢。 – 2010-11-18 10:49:04

+0

所以你有一个普通的母版页,然后是一个继承自这个的类,它是你用作母版页的*这个类?我不明白为什么你不能拥有另外一个'PageLoad'方法,毕竟,你在普通页面上有这些方法,并且它们是从'Page'继承的。如果你尝试这个,你会得到一个错误吗? – 2010-11-18 10:53:48

+0

@格拉汉姆 - 我已经更新了我的问题,你介意看一下吗? – 2010-11-18 11:55:54

1

这样的事情..

var preTag = @" <font style=""background:yellow;color:#ff0000;font-weight:600;""><b>"; 
    var postTag = " </b></font>"; 

    Environment: <%= ((this.Environment=="LIVE") ? (preTag + this.Environment + postTag) : this.Environment) %> 
1

您也可以将代码从Page_Load移动到MasterPage.master中的Page_PreRender中,它应该可以工作..它是空白的,因为MasterPage.master Page_Load覆盖了SiteMaster.Master的Page_Load,因此Environment从未被分配过。

+0

伟大的东西感谢,它现在的作品。 – 2010-11-19 10:07:21

相关问题