Pergunta

According to a few sources (including this one) when using a master page the page's Load happens before the master page's Load.

When I assign Page.Title a value in the page's Load it works.

If I write Page.Title += "J" in the master page's Load the title becomes J, regardless if a value was previously assigned.

In all cases, when I Response.Write(Page.Title) later on in the master page's Load it's empty.

What am I missing out here?

EDIT (some code):

default.aspx

Page.Title = "Title";

Master page

Page.Title += " - More title";
.
.
.
Response.Write("TITLE: " + Page.Title);

I get TITLE: in the window and - More title in the browser header.

EDIT (some debugging):

I added the following to my aspx and to the master page:

protected void Page_Init(object sender, EventArgs e)
{
    Response.Write("<!--DEBUG-" + (debug_counter++) + "- TITLE: " + Page.Title + "-->\r\n");
}
protected void Page_PreRenderComplete(object sender, EventArgs e)
{
    Response.Write("<!--DEBUG-" + (debug_counter++) + "- TITLE: " + Page.Title + "-->\r\n");
}
protected void Page_Render(object sender, EventArgs e)
{
    Response.Write("<!--DEBUG-" + (debug_counter++) + "- TITLE: " + Page.Title + "-->\r\n");
}
protected void Page_SaveStateComplete(object sender, EventArgs e)
{
    Response.Write("<!--DEBUG-" + (debug_counter++) + "- TITLE: " + Page.Title + "-->\r\n");
}

Plus a few similar lines throughout the Load function of both. They all come out blank.

Foi útil?

Solução 2

I ended up using the Page.Header.Title instead. It works.

From here I understood they're supposed to be the same. Apparently not.

Outras dicas

Master pages behave like child controls on a page: the master page Init event occurs before the page Init and Load events, and the master page Load event occurs after the page Init and Load events.

I've answered recently the question what's the best place to set a page's title from the MasterPage: https://stackoverflow.com/a/10525258/284240

you can even use SaveStateComplete event, that should be latest place where you could change the title:

protected void Page_PreRenderComplete(object sender, EventArgs e)
{
    Page.Title = "late title";
} 

protected void Page_SaveStateComplete(object sender, EventArgs e)
{
    Page.Title = "very late title";
}
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top