By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,215 Members | 1,936 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,215 IT Pros & Developers. It's quick & easy.

Page title in ASP.NET pages

P: n/a
Hello
For displaying page titles for my ASP.NET applications I use the following technique
<title><%=BannerModule.PageTitle%></title
where BannerModule.PageTitle is a public field of my user control. This field is initialized in Page_Load of every my page. This technique works with the exception that the title disappears every time a page is posted back. Could somebody explain me the reason, or suggest a better technique for setting page title at run-time?
Thanks a lot
Maxim
Nov 18 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Maxim
The page title disappears after a post back probably because you did not assign the BannerModule.PageTitle on the page post back. You have to assign that value to the control again for it to display properly. Alternatively, you can place placeholder control between the title tags and in your code you populate the title. That way, you don't have to worry about postback problem if you enable viewstate

Tu-Thac

----- Maxim Izbrodin wrote: ----

Hello
For displaying page titles for my ASP.NET applications I use the following technique
<title><%=BannerModule.PageTitle%></title
where BannerModule.PageTitle is a public field of my user control. This field is initialized in Page_Load of every my page. This technique works with the exception that the title disappears every time a page is posted back. Could somebody explain me the reason, or suggest a better technique for setting page title at run-time?
Thanks a lot
Maxim
Nov 18 '05 #2

P: n/a
Are you checking if there is a postback in the Page_Load method.

if (!IsPostBack) {
}
else {
}

You want to make sure that you set this every time.

Also you should create a user control to render out the title of the page.
This will all the pages will have the same title:

<title>
<uc1:Banner id="Banner1" runat="server"></uc1:Banner>
</title>
public class Banner : System.Web.UI.UserControl
{
protected Label Title;

private void Page_Load(object sender, System.EventArgs e)
{
Title.Text = "Hello World";
}

#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
}
http://www.xpcoder.net

"Maxim Izbrodin" <mi*******@nospamalias.com> wrote in message news:<2D**********************************@microso ft.com>...
Hello,
For displaying page titles for my ASP.NET applications I use the following technique:
<title><%=BannerModule.PageTitle%></title>
where BannerModule.PageTitle is a public field of my user control. This field is initialized in Page_Load of every my page. This technique works with the exception that the title disappears every time a page is posted back. Could somebody explain me the reason, or suggest a better technique for setting page title at run-time?
Thanks a lot,
Maxim

Nov 18 '05 #3

P: n/a
Hi Maxim,
Thanks for posting in the community!
From your description, you embed an UserControl into a webpage, the
UserControl has a Public string property so that you can use the value of
this property to set the UserControl's container page's title via such code:
<title>
<%=TitleUC1.PageTitle %>
</title>
However, you found that the page's title value will disappear after the
page is posted back.
Also, you're looking for anyother approachs to set the page's title, yes?
If there is anything I misunderstood, please feel free to let me know.

Based on my research, as for your first problem, I agree to Tu-Thach and
coder5811's suggestion that you need to set the UserControl's PageTitle
Property not only when the page is first time loaded but also when posted
back. For example, use the below code in UserControl:
if(!IsPostBack)
{
PageTitle = "Initial Title";
}
else
{
//the case when page is posted back
PageTitle = txtPageTitle.Text;
}
Thus, the title won't disappear when the page is posted back.

As for any other approachs to set the page's title, I found there is
another means to set the page's title dynamically------------set the
<titile>element as runat=server and provide an unique id, For example,
specify the titile in page as below:
.........
<HEAD> <TITLE ID="MyTitle" RUNAT="server"> </TITLE> </HEAD>
...........

and also need to declare a HtmlGeneralControl member in the codebehind
page class like:

public class TestTitle : System.Web.UI.Page
{
.......
protected System.Web.UI.HtmlControls.HtmlGenericControl MyTitle;
.............

Then, we can specify the page's title dynamically by change the MyTitle
member's InnerText property, just as:
private void Page_Load(object sender, System.EventArgs e)
{
MyTitle.InnerText = "My Custom Title!";
}

In addtion, here is weblinks to some tech artiles which have dicussed on
this topic, you may have a view if you feel any thing unclear:

#How to Change the Page Title in ASP.NET dynamically
http://www.dotnetspider.com/Technolo...px?SampleId=12

#Setting the ASP.NET Page Title and Meta Tags
http://authors.aspalliance.com/chris...sp?article=141

Please try out the preceding suggestions. If you have any questions, please
feel free to let me know.
Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Nov 18 '05 #4

P: n/a
Thank you all
I did assigned the text value to the user control member not only when the page was initially displayed, but also when it was posted back
private void Page_Load(object sender, System.EventArgs e

BannerModule.PageName = ResString.IDS_BANNER_TITLE_LOGIN()
FormsAuthentication.SignOut()

But it works as I described (the title is lost after when the page is posted back). I've tried the method suggested by Steven Cheng (<TITLE ID="MyTitle" RUNAT="server"></TITLE>) and it works. Thank you, Steven!
Nov 18 '05 #5

P: n/a
Hi Maxim,
Thanks for your prompt response. I'm very glad that my suggestion has
helped you. In addtion, if you meet any other problems or need assitance in
the furture, please always feel free to post here. I'll be willing to assit
you. Have a good day!

Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Nov 18 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.