Page title in ASP.NET pages

G

Guest

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
 
G

Guest

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
 
C

Coder Coder

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
 
S

Steven Cheng[MSFT]

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/Technology/Samples/ShowSample.aspx?SampleId=12

#Setting the ASP.NET Page Title and Meta Tags
http://authors.aspalliance.com/chrisg/default.asp?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.)
 
G

Guest

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!
 
S

Steven Cheng[MSFT]

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,902
Latest member
Elena68X5

Latest Threads

Top