代码之家  ›  专栏  ›  技术社区  ›  Mark Allison

以编程方式更改某些文本的背景色

  •  0
  • Mark Allison  · 技术社区  · 14 年前

    我用这个例子实现了母版页 How to implement a status bar in an ASP.NET application? . 我的sitemaster.cs继承了一个名为environment的母版页。在masterpage.master上,我有以下代码:

    <body>
        <form id="frmMaster" runat="server">
            <.. some content removed for brevity ...>
    
            Environment: <%= this.Environment %>
        </form>
    </body>
    

    我想做的是评估 this.Environment 如果是“实况”,则将此环境的背景色设置为红色,如果是“测试”,则将其设置为黄色。我该怎么做?

    更新 我刚将此代码添加到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.master page\u master.page\u加载(对象, System.EventArgs)'隐藏继承的 成员的“sitemaster.page”加载(对象, System.EventArgs“”。使用新的 关键字。

    更新2: 这是我在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 回复  |  直到 14 年前
        1
  •  3
  •   Graham Clark    14 年前

    而不是使用 <%= 打印环境的语法(这是使用 Response.Write )考虑使用像 Literal 或A Label . 当你想改变背景颜色时,这就意味着样式(css),所以 标签 是合适的。

    (A) 字面意义的 只是一个文本占位符,不呈现HTML,而 标签 (通常)呈现内部文本 <span> 标签)

    所以我将您的母版页标记更改为

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

    在后面的代码中,设置 Text 的属性 environmentLabel this.Environment . 同时,测试环境值,并设置 BackColor 标签的属性(或应用CSS类)。

    更新:
    对于母版页,您只需要一个类,它将从 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)都将显示标签。每次加载页面时, Page_Load 将调用sitemaster.master.cs中的事件,并更新文本。你不需要定义 MasterPage 类自己,这是由.NET框架提供的。

    你可能想改进这个 页面加载 方法,或者使用viewstate,因此仅在不进行回发时设置文本,或者通过在 环境标签 控制。

    最后,您的站点中会有一个或多个ASPX页面,在标记的顶部会显示如下内容:

    <%@ Page Title="" Language="C#" MasterPageFile="~/SiteMaster.Master" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication1.WebForm1" %>
    
        2
  •  1
  •   RameshVel    14 年前

    像这样的……

       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)   %>
    
        3
  •  1
  •   Shadow Wizard    14 年前

    您还可以在master page.master中将代码从page_load移到page_prerender,它应该可以工作。它是空白的,因为master page.master page_load覆盖了sitemaster的page_load。master因此从未分配环境。