更新面板异步回发后页面标题消失

15

我最近才注意到,从我的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在标签中的<form>标签之后的第一件事就被放置了。

<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>
6个回答

7

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

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

显然,当ajax调用发生时,它会重新运行主页面。这导致我们的标题消失。

例如:

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

更好的解决方法是放弃使用MS UpdatePanel,开始使用JSON / jQuery,在调用方面实际上有一些不错的控制。

6
你是否反对使用Content Page的Title属性?
<%@ Page Title="Your Page Title" Language="vb" AutoEventWireup="false" MasterPageFile="~/MasterPages/...

您可以在页面加载时以编程方式访问此功能...

3

如果您删除标题标记中的空格,可以解决一个奇怪的错误。

例如:

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

已在SharePoint 2010上进行测试


1

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

0

当您以编程方式设置标题且仅在非PostBack时发生此情况。只需重写保存/加载PostBack方法以将标题保存在ViewState中即可。

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

-2
你可以将页面标题放在Viewstate中,然后只需在按钮回发的Click事件中获取该字符串并将其赋值给Page.Title。
    public string MyPageTitle
    {
        get
        {
            return (string)ViewState["MyPageTitle"];
        }
        set
        {
            ViewState["MyPageTitle"] = value;
        }
    }

在页面加载时分配:MyPageTitle =“我的酷网页标题”; 然后在按钮单击事件中:
protected void MyLinkButton_Click(object sender, EventArgs e)
    {

       Page.Title = MyPageTitle;

    }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接