views:

1537

answers:

4

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>
+3  A: 

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.

Chris Lively
+4  A: 

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

TGnat
Thank you for making me feel like an idiot :)
TheTXI
I had the same experience last week...
TGnat
A: 

I have the same problem, but what if I want different titles for certain pages? should I do the same in each Page Load event?

Anyway, I liked: "drop the MS updatepanel crap" nobody using that because he likes it, it's just because there's no time to do it the right way, so you had to do it the easy(and wrong) way.

A: 

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;

    }
developerz