UpdatePanel 异步回发后页面丢失标题
我最近刚刚注意到,在我从主页的 UpdatePanel
内部执行异步回发后,我的页面标题将重置为标准的“无标题页面”。 从母版页内部回发期间(例如当我单击母版页内的搜索框按钮时),标题不会丢失。
我认为通过专门使用不同的 contentplaceholder
来设置文档标题,我将避免此类问题,但显然我错了。 除了必须在 ASPX 页面的代码隐藏中显式设置标题(我希望通过下面的设置方式避免这种情况)之外,我还缺少其他什么吗?
这是我的页面的基本要点,它调用母版页(下面的母版页代码)
<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>
这是母版页的。 ASP.NET AJAX ScriptManager 首先放置在正文中
<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>
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>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
我们在我们的一个网站上遇到了这个问题。
立即修复是在 page_load 方法后面的母版页代码中重置标题。
显然,当 ajax 调用发生时,它正在重新运行母版页。 这导致我们的头衔消失。
例如:
更好的解决方法是放弃 MS updatepanel 垃圾并开始使用 JSON / jQuery,这样您实际上可以对调用进行一些适当的控制。
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:
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.
您是否反对使用内容页的标题属性?
您还可以在页面加载中以编程方式访问此...
Are you opposed to using the Title property of the Content Page?
You can also access this programmatically in the page load...
这是一个奇怪的错误,如果您删除标题标签中的空格,则可以解决该错误,例如:
在 Sharepoint 2010 上测试
Is a weird bug that can be workedaround if you remove the spaces in the title tag like:
Tested on Sharepoint 2010
与其更改服务器端代码,为什么不在 JS 中修复它:
Rather than change your server side code, why not just fix it in JS:
当您以编程方式设置标题并且仅当不是 PostBack 时,才会发生这种情况。 只需重写保存/加载回发方法以将标题保存在视图状态包中。
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.
您可以将页面标题放入 Viewstate 中,然后抓取按钮回发 Click 事件中的字符串并将其分配给 Page.Title
On Page load 分配: MyPageTitle = "My Cool Web Page Title";
然后在按钮点击事件中:
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
On Page load assign: MyPageTitle = "My Cool Web Page Title";
Then in button click event: