UpdatePanel 异步回发后页面丢失标题

发布于 2024-07-15 07:54:51 字数 1573 浏览 4 评论 0原文

我最近刚刚注意到,在我从主页的 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 技术交流群。

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

发布评论

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

评论(6

━╋う一瞬間旳綻放 2024-07-22 07:54:51

我们在我们的一个网站上遇到了这个问题。

立即修复是在 page_load 方法后面的母版页代码中重置标题。

显然,当 ajax 调用发生时,它正在重新运行母版页。 这导致我们的头衔消失。

例如:

protected void Page_Load(object sender, EventArgs e) {
    this.Page.Title = "whatever title you have...";
}

更好的解决方法是放弃 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:

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.

一个人的旅程 2024-07-22 07:54:51

您是否反对使用内容页的标题属性?

<%@ Page Title="Your Page Title" Language="vb" AutoEventWireup="false" MasterPageFile="~/MasterPages/...

您还可以在页面加载中以编程方式访问此...

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...

非要怀念 2024-07-22 07:54:51

这是一个奇怪的错误,如果您删除标题标签中的空格,则可以解决该错误,例如:

<title><asp:ContentPlaceHolder id="title" runat="server"></asp:ContentPlaceHolder></title>

在 Sharepoint 2010 上测试

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

挖个坑埋了你 2024-07-22 07:54:51

与其更改服务器端代码,为什么不在 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;
        });
});

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;
        });
});
檐上三寸雪 2024-07-22 07:54:51

当您以编程方式设置标题并且仅当不是 PostBack 时,才会发生这种情况。 只需重写保存/加载回发方法以将标题保存在视图状态包中。

    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;
    }

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;
    }
酒几许 2024-07-22 07:54:51

您可以将页面标题放入 Viewstate 中,然后抓取按钮回发 Click 事件中的字符串并将其分配给 Page.Title

    public string MyPageTitle
    {
        get
        {
            return (string)ViewState["MyPageTitle"];
        }
        set
        {
            ViewState["MyPageTitle"] = value;
        }
    }

On Page load 分配: MyPageTitle = "My Cool Web Page Title";
然后在按钮点击事件中:

protected void MyLinkButton_Click(object sender, EventArgs e)
    {

       Page.Title = MyPageTitle;

    }

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;

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