如何实现在ASP.NET应用程序状态栏? [英] How to implement a status bar in an ASP.NET application?

查看:82
本文介绍了如何实现在ASP.NET应用程序状态栏?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个叫MasterPage.master母版页一个简单的ASP.NET应用程序。在MasterPage.master我有一个名为 lblStatus 标签。我想,当某些事件的发生网站更新此标签的文本,总是不管显示最新的状态我是在网站,网页。

I have a simple ASP.NET application which has one master page called MasterPage.master. In MasterPage.master I have a Label called lblStatus. I want to update the text of this label when certain events happen in the website and always display the latest status no matter which web page I am on in the site.

这是我在想什么做的,但我不能确定这是否是最优雅的:

This is what I'm thinking of doing, but I'm unsure if this is the most elegant:


  1. 创建与code更新会话状态的一个新的类文件会话[状态]

  2. 调用一个方法在这个新的类文件,每当有事情发生,更新会话状态

  3. 在MasterPage.master页面的pageLoad的事件,更新 lblStatus.Text 会话[状态]

  1. Create a new class file with the code to update session state Session["status"]
  2. Call a method in this new class file to update Session state whenever something happens
  3. In the PageLoad event of the MasterPage.master page, update the lblStatus.Text with Session["status"]

问题


  1. 我应该使用文字代替标签的

  2. 它会更好做另一种方式?

谢谢,这是我的code至今MasterPage.master:

Thanks, here's my code so far in MasterPage.master:

<%@ Master Language="C#" %>

<script runat="server">       
    public string StatusText
    {
        get { return ltlStatus.Text; }
        set { ltlStatus.Text = value; }        
    }    
</script>

<html>
<head runat="server" id="Header">
    <asp:contentplaceholder id="ContentPlaceHolderScript" runat="server" />
    <title>MooDB - </title>
</head>
<body>
    <form id="frmMaster" runat="server">
    <div>        
        <asp:Menu ID="mnuNav" Orientation="Horizontal" runat="server">
        <.. removed for brevity ..>
        </asp:Menu>
    </div>
    <div>
        <asp:Literal ID="ltlStatus" runat="server" />
    </div>
    </form>
</body>
</html>

我不能确定下一位的。我需要这个code在我的网站复制到每一个页面:?

I'm unsure of the next bit. Do I need to copy this code onto every page in my site:?

<%@ Page Language="C#" %>

<script runat="server">

    private void ChangeStatus(string newStatus)
    {
        ((SiteMaster)this.Master).StatusText = newStatus;
    }

</script>    

<asp:Content ContentPlaceHolderID="contentMain" runat="server" ID="Main" >

    <... my page content ...>
</asp:Content>

对不起,我是新来的ASP.NET和我不知道该怎么办。什么是SITEMASTER?

Sorry I'm new to ASP.NET and I'm not sure what to do. What is SiteMaster?

推荐答案

回答问题1:

恕我直言,这其实并不重要。我会说这是更清洁的使用文字当你不需要一个标签指向表单字段,但是,好了,大多数人使用标签在任何情况下。或者直接用文本输出&LT; %%方式&gt;

IMHO, it really doesn't matter. I would say it's cleaner to use Literal when you don't need a label pointing to a field of a form, but, well, most people use Label in every case. Or output text directly with <% %>.

回答问题2:

有没有必要使用会话此。相反,使用 this.Master 直接访问母版。然后,您可以更新您的网页标签的价值。

There is no need to use sessions for this. Instead, use this.Master to access the Masterpage directly. You can then update the value of the label from your page.

例如,做这样的事情:

public class SiteMaster : MasterPage
{
    public string StatusText { get; set; }
}

public class HomePage : Page
{
    public void ChangeStatus(newStatus)
    {
        // Remember to cast this.Master to the class you use for the masterpage.
        ((SiteMaster)this.Master).StatusText = newStatus;
    }
}


要回答你的最后一个问题:


To answer your last questions:

我需要这个code在我的网站复制到每一页?

Do I need to copy this code onto every page in my site?

是的。的除非的每一页,从从类。

Yes. Unless every page inherits from a parent class which inherits from Page class.

什么是SITEMASTER?

What is SiteMaster?

SITEMASTER 是类的 I 的给我的母版页。这可能是模板全球你想做

SiteMaster is the class I gave to my master page. It could be Template, Global, or whatever you want.

如果它可能会有所帮助,这里的东西全例如:

If it might be helpful, here's a full example of the thing:

文件的Site.Master

<%@ Master Language="C#" AutoEventWireup="true" CodeBehind="Site.master.cs" Inherits="Demos.StackOverflow.MasterPages.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>Sample code for StackOverflow</title>
</head>
<body>
    <form id="form1" runat="server">
    <a href="Default.aspx">Home page</a> | <a href="Products.aspx">Products</a>
    <div>
        <asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">
        </asp:ContentPlaceHolder>
    </div>
    <div id="Status">
        <%= this.StatusText %>
    </div>
    </form>
</body>
</html>

文件 Site.Master.cs

namespace Demos.StackOverflow.MasterPages
{
    using System;
    using System.Web.UI;

    // Here's my SiteMaster class.
    public partial class SiteMaster : MasterPage
    {
        public string StatusText { get; set; }

        protected void Page_Load(object sender, EventArgs e)
        {
        }
    }
}

文件 PageWithStatus

namespace Demos.StackOverflow.MasterPages
{
    using System.Web.UI;

    // This parent class inherits System.Web.UI.Page. This avoids code duplication.
    public class PageWithStatus : Page
    {
        protected void ChangeStatus(string newStatus)
        {
            ((SiteMaster)this.Master).StatusText = newStatus;
        }
    }
}

文件 Default.aspx的

<%@ Page Title="" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true"
    CodeBehind="Default.aspx.cs" Inherits="Demos.StackOverflow.MasterPages.DefaultPage" %>

<asp:Content ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
    <asp:Button runat="server" Text="Button" OnClick="DefaultButton_Click" />
</asp:Content>

文件 Default.aspx.cs

namespace Demos.StackOverflow.MasterPages
{
    using System;

    // Here, we inherits from PageWithStatus, not from Page.
    public partial class DefaultPage : PageWithStatus
    {
        protected void Page_Load(object sender, EventArgs e)
        {
        }

        protected void DefaultButton_Click(object sender, EventArgs e)
        {
            this.ChangeStatus("Done!");
        }
    }
}

文件 Products.aspx

<%@ Page Title="" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true"
    CodeBehind="Products.aspx.cs" Inherits="Demos.StackOverflow.MasterPages.ProductsPage" %>

<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
    <asp:Button runat="server" Text="Buy" onclick="BuyButton_Click" /><br />
    <asp:Button runat="server" Text="Sell" onclick="SellButton_Click" />
</asp:Content>

文件 Products.aspx.cs

namespace Demos.StackOverflow.MasterPages
{
    using System;

    // Here, we inherits from PageWithStatus, not from Page.
    public partial class ProductsPage : PageWithStatus
    {
        protected void Page_Load(object sender, EventArgs e)
        {
        }

        protected void BuyButton_Click(object sender, EventArgs e)
        {
            this.ChangeStatus("The product is purchased.");
        }

        protected void SellButton_Click(object sender, EventArgs e)
        {
            this.ChangeStatus("The product is sold.");
        }
    }
}

这篇关于如何实现在ASP.NET应用程序状态栏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆