【问题标题】:How to change the Page Title in ASP.Net 1.1?如何更改 ASP.Net 1.1 中的页面标题?
【发布时间】:2009-07-30 12:32:58
【问题描述】:

使用 ASP.Net 2.0,您可以使用 Title 属性来更改页面标题:

Page.Title = "New Title";

但是由于在 ASP.Net 1.1 中 Page 类中没有 Title 属性,我如何从代码隐藏中更改页面标题?

【问题讨论】:

    标签: c# webforms asp.net-1.1 page-title


    【解决方案1】:

    使用 ASP.Net 1.1,首先您必须在标题标记上设置 runat 属性:

    <title id="PageTitle" runat="server">WebForm1</title>
    

    然后从后面的代码:

    C#

    // We need this name space to use HtmlGenericControl
    using System.Web.UI.HtmlControls;
    
    namespace TestWebApp
    {
    
          public class WebForm1 : System.Web.UI.Page
          {
                // Variable declaration and instantiation
                protected HtmlGenericControl PageTitle = new HtmlGenericControl();
    
                private void Page_Load(object sender, System.EventArgs e)
                {
                      // Set new page title
                      PageTitle.InnerText = "New Page Title";
                }
          }
    }
    



    VB

    Imports System.Web.UI.HtmlControls
    
    Namespace TestWebApp
    
        Public Class WebForm1
            Inherits System.Web.UI.Page
    
            Protected PageTitle As HtmlGenericControl = New HtmlGenericControl()
    
            Private Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
    
                PageTitle.InnerText = "New Page Title"
            End Sub
    
    ...
    
        End Class
    End Namespace
    

    【讨论】:

      【解决方案2】:

      Andreas Grech 的答案在从具有 TITLE 标签的 ASPX 页面后面的代码中运行时效果很好。

      但是如果需要从 ASPX 页面运行的 Web 用户控件 更新 TITLE 标记怎么办?以上会导致错误(因为网页标题对 Web 用户控件不可见)。

      因此,对于 Web 用户控件,请按照 Grech 的解决方案进行操作,但要进行以下调整:

      1) 不要在 Page_Load 之前声明 PageTitle 控件。而是:

      2) 在Page_Load中声明如下:

      Dim PageTitle as HtmlGenericControl = Page.FindControl("PageTitle")

      然后设置值。

      【讨论】:

        【解决方案3】:

        这里的要点是,如果您在母版页中设置标题

        <head><title>Master Title</title></head>
        

        您在代码端添加标题的代码将不起作用。即使一切都正确

        Page.Title="Page Title"
        

        上面这个无效。您必须从母版页中删除标题。之后不需要额外的代码。只需在Page_Load中添加此代码

        Page.Title="Page Title"
        

        它会起作用的

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-01-16
          • 2021-03-23
          • 1970-01-01
          • 2013-05-27
          相关资源
          最近更新 更多