如何更改 ASP.Net 1.1 中的页面标题?

发布于 2024-07-30 12:36:42 字数 200 浏览 11 评论 0原文

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

Page.Title = "New Title";

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

With ASP.Net 2.0 you can use the Title property to change the page title :

Page.Title = "New Title";

But since in ASP.Net 1.1 there isn't a Title property in the Page class, how can I change the page's title from the code-behind ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

ら栖息 2024-08-06 12:36:42

使用 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

With ASP.Net 1.1, first you have to set the runat attribute on the title markup :

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

Then from the code behind :

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
迷你仙 2024-08-06 12:36:42

当从具有 TITLE 标签的 ASPX 页面后面的代码运行它时,Andreas Grech 的答案非常有效。

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

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

1) 不要在 Page_Load 之前声明 PageTitle 控件。 相反:

2) 在 Page_Load 中声明它,如下所示:

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

然后设置值。

The answer by Andreas Grech works very well when running it from the code behind of the ASPX page that has the TITLE tag.

But what if the TITLE tag needs to be updated from a Web User Control ran from the ASPX page? The above would result in an error (because PageTitle is not visible to the Web User Control).

So in the case of a Web User Control, do as Grech's solution dictates, but make the following adjustments:

1) Do not declare the PageTitle control before Page_Load. Instead:

2) Declare it inside Page_Load as follows:

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

And then set the value.

久伴你 2024-08-06 12:36:42

这里的要点是,如果您在

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

代码中的母版页中设置标题,则在代码端添加标题将不起作用。即使一切都是正确的,

Page.Title="Page Title"

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

Page.Title="Page Title"

即可工作

In here main point is that if you set title in your master page in

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

Your code to add title in code side will not work.Even everything is correct

Page.Title="Page Title"

This one above not effective. You have to remove title from master page. After that no need to extra code.Just add this code below in Page_Load

Page.Title="Page Title"

And it will work

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文