问题
I have just noticed recently that my page title will reset to the standard "Untitled Page" after I perform an asyncpostback from inside my UpdatePanel
in the main page. The title will not be lost during a postback from inside the master page (such as when I click on the search box button inside the master page).
I assumed that by using a different contentplaceholder
specifically for setting the document title I was going to avoid issues like this, but apparently I was wrong. Is there something else I am missing other than having to explicitly set the title in the code-behind of the ASPX page (which I was hoping to avoid with the way it was setup below)?
Here is the basic gist of my page which is calling the Master Page (master page code below)
<asp:Content ID="Content1" ContentPlaceHolderID="title" Runat="Server">
Page Title
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="head" Runat="Server">
<script type="text/javascript">
//random javascript validators
</script>
</asp:Content>
<asp:Content ID="Content3" ContentPlaceHolderID="content" Runat="Server">
<div class="title">
Account Management
</div>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
//Username + Password Set Form
</ContentTemplate>
</asp:UpdatePanel>
</asp:Content>
This is the of the Master Page. The ASP.NET AJAX ScriptManager is placed first thing after the <form>
tag in the body.
<head id="Head1" runat="server">
<title>
<asp:ContentPlaceHolder id="title" runat="server">
</asp:ContentPlaceHolder>
</title>
//Stylesheet references
<script type="text/javascript">
//Random javascript functions
</script>
<asp:ContentPlaceHolder id="head" runat="server">
</asp:ContentPlaceHolder>
</head>
回答1:
Are you opposed to using the Title property of the Content Page?
<%@ Page Title="Your Page Title" Language="vb" AutoEventWireup="false" MasterPageFile="~/MasterPages/...
You can also access this programmatically in the page load...
回答2:
We ran into this exact issue on one of our sites.
The immediate fix was to reset the title in the master page codebehind page_load method.
Apparently when the ajax call occurs, it is rerunning the master page. This was causing our title to disappear.
For example:
protected void Page_Load(object sender, EventArgs e) {
this.Page.Title = "whatever title you have...";
}
A better fix is to drop the MS updatepanel crap and start using JSON / jQuery where you actually have some decent control over the calls.
回答3:
Is a weird bug that can be workedaround if you remove the spaces in the title tag like:
<title><asp:ContentPlaceHolder id="title" runat="server"></asp:ContentPlaceHolder></title>
Tested on Sharepoint 2010
回答4:
Rather than change your server side code, why not just fix it in JS:
$(function(){
var prm = Sys.WebForms.PageRequestManager.getInstance();
if (!(prm)) return;
document.orginalTitle=document.title;
prm.add_endRequest(function(s, e){
if (document.title.replace(/\s/g,"").length==0)
document.title=document.orginalTitle;
});
});
回答5:
It happens when you set the title progammatically and only when is not PostBack. Just rewrite save/load postback methods to hold the title in the viewstate bag.
protected override void LoadViewState(object savedState)
{
object[] allStates = (object[])savedState;
if (allStates[0] != null)
base.LoadViewState(allStates[0]);
if (allStates[1] != null)
Page.Title = (string)allStates[1];
}
protected override object SaveViewState()
{
object[] allStates = new object[2];
object baseState = base.SaveViewState();
string pageTitle = Page.Title;
allStates[0] = baseState;
allStates[1] = pageTitle;
return allStates;
}
回答6:
You could put the Page title in Viewstate and then just grab the string in the button postback Click event and assign it to Page.Title
public string MyPageTitle
{
get
{
return (string)ViewState["MyPageTitle"];
}
set
{
ViewState["MyPageTitle"] = value;
}
}
On Page load assign: MyPageTitle = "My Cool Web Page Title"; Then in button click event:
protected void MyLinkButton_Click(object sender, EventArgs e)
{
Page.Title = MyPageTitle;
}
来源:https://stackoverflow.com/questions/627378/page-losing-title-after-updatepanel-asyncpostback