在 ASP.Net 2.0 中,您可以使用 Title 属性来更改页面标题:
Page.Title = "New Title";
但由于在 ASP.Net 1.1 中 Page 类中没有 Title 属性,我如何从代码隐藏更改页面的标题?
请您参考如下方法:
对于 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
