C# 如何在页面加载时动态更改aspx页面的标题
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16982811/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to change title of aspx page dynamically on page load
提问by Randhi Rupesh
I had set of ASPX pages in which each page had different titles, but I want to put default title for pages which don't have a title. The default title must be configurable.
我有一组 ASPX 页面,其中每个页面都有不同的标题,但我想为没有标题的页面设置默认标题。默认标题必须是可配置的。
采纳答案by gzaxx
If this is classic ASP.NET (not MVC) and you are using MasterPagethen you can set default title in Page_Loadevent in MasterPage:
如果这是经典的 ASP.NET(不是 MVC)并且您正在使用,MasterPage那么您可以在Page_Load事件中设置默认标题MasterPage:
protected void Page_Load(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(Page.Title))
{
Page.Title = ConfigurationManager.AppSettings["DefaultTitle"]; //title saved in web.config
}
}
回答by Raheel Khan
In your master page code behind, you could set [this.Title = "Whatever";]or you could also specify the default title in the HTML.
在后面的母版页代码中,您可以设置[this.Title = "Whatever";]或也可以指定 HTML 中的默认标题。
回答by Chris N.P.
You can do this:
你可以这样做:
Set the aspx header something like this
像这样设置 aspx 标头
<HEAD>
<TITLE ID=CaptionHere RUNAT="server"></TITLE>
</HEAD>
And in code behind put this inside the page load event:
在后面的代码中把它放在页面加载事件中:
if(!IsPostBack)
{
myCaption.InnerHtml = "Hope this works!"
}
I hope this will help you
我希望这能帮到您
回答by Kanhaiya lal Rajora
protected void Page_Load(object sender, EventArgs e)
{
Page.Title = title();
}
private string title()
{
SqlConnection con = new SqlConnection(cs);
string cmdstr = "select * from title where id = 2";
SqlCommand cmd = new SqlCommand(cmdstr, con);
DataTable dt = new DataTable();
SqlDataAdapter da = new SqlDataAdapter(cmd);
con.Open();
da.Fill(dt);
con.Close();
if (dt.Rows.Count > 0)
{
string title = dt.Rows[0]["title"].ToString();
}
return title;
}
This is helpful
这很有帮助
回答by krowe2
I had a similar problem and none of these solutions worked well for me. The problem stems from the order control events fire for a page. In my case, I had some code that needed to be in the Page_load event (this was because that is the first event where we have a Request object to work with). That code also needed to run before the title could be set. Other pages in my site were able to simply set the desired Title in the page Ctor but because this page needed to interrogate the response object for information first, it was a problem. The problem with this is that the master page has already created the page header section by the time we get to the Page_load event and I didn't want junk in my Master page that was only required for a single page on my site. My simple hack to overcome this issue was to insert a bit of javascript inline in the content portion of the page:
我有一个类似的问题,这些解决方案都不适合我。问题源于页面的顺序控制事件触发。在我的例子中,我有一些代码需要在 Page_load 事件中(这是因为这是我们有一个 Request 对象要处理的第一个事件)。该代码还需要在设置标题之前运行。我站点中的其他页面能够简单地在页面 Ctor 中设置所需的标题,但是因为该页面需要首先询问响应对象的信息,所以这是一个问题。问题在于母版页在我们到达 Page_load 事件时已经创建了页眉部分,我不希望母版页中只有我网站上的单个页面需要的垃圾。
<asp:Content ID=BodyContent ContentPlaceHolderID=MainContent RunAt=Server>
<script type="text/javascript">
document.title='<%=Title%>';
</script>
... the rest of the content page goes here ...
</asp:Content>
With this in place, you are free to set the Title in the Page_Load event and it'll be set as soon as this line of code has downloaded. Of course, my site already has a JS requirement so if you're trying to avoid that then this is not going to work for you.
有了这个,你就可以在 Page_Load 事件中自由设置标题,一旦这行代码下载,它就会被设置。当然,我的网站已经有一个 JS 要求,所以如果你想避免这种情况,那么这对你来说是行不通的。

