Page losing title after UpdatePanel asyncpostback

感情迁移 提交于 2019-12-03 10:43:57

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

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.

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

Martin

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

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

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;

    }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!