Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > Page title in ASP.NET pages

Reply
Thread Tools

Page title in ASP.NET pages

 
 
=?Utf-8?B?TWF4aW0gSXpicm9kaW4=?=
Guest
Posts: n/a
 
      02-02-2004
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
 
Reply With Quote
 
 
 
 
=?Utf-8?B?VHUtVGhhY2g=?=
Guest
Posts: n/a
 
      02-02-2004
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
 
Reply With Quote
 
 
 
 
Coder Coder
Guest
Posts: n/a
 
      02-02-2004
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" <(E-Mail Removed)> wrote in message news:<(E-Mail Removed)>...
> 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

 
Reply With Quote
 
Steven Cheng[MSFT]
Guest
Posts: n/a
 
      02-03-2004
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.)

 
Reply With Quote
 
=?Utf-8?B?TWF4aW0gSXpicm9kaW4=?=
Guest
Posts: n/a
 
      02-03-2004
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!
 
Reply With Quote
 
Steven Cheng[MSFT]
Guest
Posts: n/a
 
      02-03-2004
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.)

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
I'm looking for html cleaner. Example : convert <h1><span><font>my title</font></span></h1> => <h1>my title</h1>… Stéphane Klein Python 2 03-30-2010 12:35 AM
Friendly Urls with page title in (eg. page-title.aspx) anthonykallay@googlemail.com ASP .Net 1 04-24-2007 08:25 AM
How 2 place page title into window title using sitemap in master page? bednar.tomas@gmail.com ASP .Net 0 11-30-2006 03:17 PM
Does anyone know the title of the first ever DVD title released? (UK/US) Mikey DVD Video 12 09-23-2006 07:14 PM
Fastest way to get a the string between <title> </title> Andreas Klemt ASP .Net 1 08-10-2003 01:58 AM



Advertisments