日期:2013-09-11  浏览次数:20586 次

图 1:浏览器中呈现的标准页眉和页脚示例
这是一个简单的示例,但您可以很容易地将它扩展,使其包含标准的页眉与导航,或者仅输出相应的 <!-- #include ---> 语句。请注意,如果希望页眉或页脚包含交互内容,应考虑使用 ASP.NET 用户控件。

[SomePage.aspx 源代码 - 内容示例]

<FONT face="Arial" color="#cc66cc" size="5">常规页面内容</FONT>

[Visual Basic Global.asax]

<%@ Application Language="VB" %><script runat="server">      Sub Application_BeginRequest(sender As Object, e As EventArgs)         ' 生成页眉         Context.Response.Write("<html>" + ControlChars.Lf + _"<body bgcolor=#efefef>" + ControlChars.Lf + "<hr>" + _ ControlChars.Lf)      End Sub                   Sub Application_EndRequest(sender As Object, e As EventArgs)         ' 生成页脚         Context.Response.Write("<hr>" + ControlChars.Lf + _      "2002 Microsoft Corporation 版权所有" + _      ControlChars.Lf + "</body>" + ControlChars.Lf + "</html>")      End Sub </script>

[C# Global.asax]

<%@ Application Language="C#" %><script runat="server">        void Application_BeginRequest(Object sender, EventArgs e) {            // 生成页眉            Context.Response.Write("<html>\n<body bgcolor=#efefef>\n<hr>\n");        }        void Application_EndRequest(Object sender, EventArgs e) {            // 生成页脚            Context.Response.Write("<hr>\2002 Microsoft Corporation 版权所有\n");            Context.Response.Write("</body>\n</html>");        }</script>

如何在用户经过身份验证后显示欢迎信息?


回答:测试 User 上下文对象以查看用户是否经过身份验证。如果是,还要从 User 对象获取用户名。当然,这是本文开头的示例。

[Visual Basic]

<script language="VB" runat="server">    Sub Page_Load(sender As Object, e As EventArgs) {        If User.Identity.IsAuthenticated Then            welcome.Text = "欢迎" + User.Identity.Name        Else            ' 尚未登录,添加一个指向登录页的链接            welcome.Text = "请登录!"            welcome.NavigateUrl = "signin.aspx"        End If    End Sub</script><asp:HyperLink id="welcome" runat="server" maintainstate="false"></asp:HyperLink>

[C#]

<script language="C#" runat="server">    void Page_Load(object sender, EventArgs e) {        if (User.Identity.IsAuthenticated) {            welcome.Text = "欢迎" + User.Identity.Name;