Dynamically changing the title attribute asp.net

Hi

I'm looking for a way of changing the title attribute on an ASPX page using c# and making the change in the code behind the page during the page_load function.

I've been round and round the houses but haven't found anything that works for me yet.

Please help!

Thanks
Simon
LVL 2
sbennettsAsked:
Who is Participating?
 
TornadoVConnect With a Mentor Commented:
Try this:

Put this in your base page ASPX:

< title runat="server" id="pageTitle" > < / title >

Put this in your base page ASPX Code-behind:

protected System.Web.UI.HtmlControls.HtmlGenericControl pageTitle;

public string PageTitle
{
   get { return pageTitle.InnerText; }
   set { pageTitle.InnerText = value; }
}

Put this in your Page_Load event of your form:
private void Page_Load(object sender, System.EventArgs e)
{
      YourBasePageClassName.PageTitle = "Item name that your customer is currently viewing";
}


And then you don't even need to use System.Web.UI.Control form = Page.Controls[1]; line.
0
 
Sam_JayanderCommented:
Hi Simon,

I don't know whether there is a proper way to do this.  But here is a work around.

1. Create a public variable in code behind
ex: public string pagetitle = "test";

2. On page load assign the title you want to that variable,
ex: pagetitle = "Changed Page Title";

3. Assign the value of that variable to the title property of the target web page.
ex: <title><%=pagetitle%></title>

Hope this will help you.

Regards,
Sam.
0
 
raterusCommented:
if you are using ASP.net 2.0, you can set the page title like this

Page.Title = "My Title"

You do have to have a <head runat="server"> in your aspx or masterpage though.
0
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

 
TornadoVCommented:
Or you can simply put this in your page_load event:

string myNewTitle = "Your new title";

Title = myNewTitle;
0
 
sbennettsAuthor Commented:
Hi Sam

That solution would work, however I have a base class page which adds user controls such as a header, subheader and footer.

If I add your suggestion to one of my pages, it fails rendering the page on the line:
System.Web.UI.Control form = Page.Controls[1];

Reckons the index is out of range.  I've tried changing the 1 but still nothing.

Any thoughts?

Thanks
Simon
0
 
sbennettsAuthor Commented:
I'm using VS2003
0
 
raterusCommented:
You are going to have to communicate with your base page to manipulate the Title, depending on what exactly you are doing, this could be easy...or this could be hard.  You 'could' just write javascript to change the page title when it loads, but it won't actually be presented to the end user like <title>My Page Title</title> in the HTML source.
0
 
TornadoVCommented:
ASPX:

< title runat="server" id="pageTitle" > < / title >

ASPX Code-behind:

protected System.Web.UI.HtmlControl.HtmlGenericControl pageTitle;

public string PageTitle
{
   get { return pageTitle.InnerText; }
   set { pageTitle.InnerText = value; }
}

ASCX Code-behind

ASPXClassName.PageTitle = "someValue";

0
 
sbennettsAuthor Commented:
It's a kinda e-commerce site and I want to change the page to say the item the customer is viewing.  That way it'll help in the search engines etc etc.  I've a feeling it's going to be hard.  How can I do it?

Basically I create my base page which adds the header and menus etc.  I'd then like to interrogate my db to get details of the product the customer is viewing and then change the title accordingly.

Hope that makes sense.

Simon
0
 
TornadoVCommented:
Put this in your ASPX:

< title runat="server" id="pageTitle" > < / title >

Put this in your ASPX Code-behind:

protected System.Web.UI.HtmlControls.HtmlGenericControl pageTitle;

public string PageTitle
{
   get { return pageTitle.InnerText; }
   set { pageTitle.InnerText = value; }
}

Put this in your Page_Load
private void Page_Load(object sender, System.EventArgs e)
{
      this.PageTitle = "Item name that your customer is currently viewing";
}

This should work fine.
0
 
sbennettsAuthor Commented:
Hi TornadoV

I have the same problem as before with this code.  My base page then decides to render my entire page into the title when it gets to the:
System.Web.UI.Control form = Page.Controls[1];
line.
0
 
sbennettsAuthor Commented:
The System.Web.UI.Control form = Page.Controls[1]; line is part of my base page which is to load the header / menu / footer etc.

The page on it's own will work with your suggested code, but not in the app :-(
0
 
TornadoVCommented:
Two suggestions:

1. Increase amount of points for this question :)
2. Post your source code.  

Just to verify, your code runs fine without setting a title and once you implement setting of the title then System.Web.UI.Control form = Page.Controls[1]; line breaks, correct?
0
 
sbennettsAuthor Commented:
Yes.  My code works fine without the title code.  The pages are rendered correctly from the base page.  Also your code works in a stand-alone page.  As soon as I change the base page to my base page, it fails.

My base page has the following code in it:
protected override void Render(System.Web.UI.HtmlTextWriter writer)
{
      System.Web.UI.Control form = Page.Controls[1];

      namespace.common.header Header = null;
      Header = (header)LoadControl(Request.ApplicationPath + "/common/header.ascx");
      form.Controls.AddAt(0, Header);
      
      form.Controls.AddAt(1, new subheader());

      namespace.common.footer Footer = null;      

      Footer = (footer)LoadControl(Request.ApplicationPath + "/common/footer.ascx");
      form.Controls.AddAt(Page.Controls[2].Controls.Count,Footer);
      base.Render (writer);
}

My asp has nothing in it at all, it's just a test page but looks like this...
<%@ Page language="c#" Codebehind="title.aspx.cs" AutoEventWireup="false" Inherits="siesta.title" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<html>
      <head>
            <title runat="server" id="pageTitle"></title>
      </head>
      <body>
            <asp:panel id="bodypanel" runat="server" cssclass="container"></asp:panel>
      </body>
</html>

Code behind ASPX page is:
public class title : common.mybase
{
      protected System.Web.UI.HtmlControls.HtmlGenericControl pageTitle;
            public string PageTitle
      {
            get { return pageTitle.InnerText; }
            set { pageTitle.InnerText = value; }
      }
      
      private void Page_Load(object sender, System.EventArgs e)
      {
            this.PageTitle = "Hello test";
      }
}

I have a few more points I'm happy add if it's gonna help? :-)
0
 
sbennettsAuthor Commented:
There, taken both suggestions :-) how good am I?

Any thoughts?
0
 
raterusCommented:
I guess upgrading to asp.net 2.0 is out of the question...  All these problems go away quickly when you use masterpages.
0
 
sbennettsAuthor Commented:
How easy is an upgrade?  

Is there any other way?
0
 
raterusCommented:
Well, right now you have two paths, you can tinker with this base page yuck, which you can get working if you mess with it long enough, don't get me wrong.

Or you can upgrade to asp.net 2.0, use masterpages for your site layout, and set the Page Title using some of the first suggestions you see on this question.  Not to mention the other 1001 benefits of using some of the new components of asp.net 2.0 that you don't know you need yet!

However, if you are a VS 2003 user, you will have to upgrade to either V$ 2005, or you can use the free express versions, which do mostly the same thing, for free.

Just an idea...
0
 
sbennettsAuthor Commented:
I've not used VS2005 yet.  What sort of overhead am I then going to have converting / re-writing?  Is there a conversion or am I having to re-write anyway?

Anything I can read that shows the basics of what's involved to give me an idea?

In the meantime, I'd still like to get it working the hard way now guys...
0
 
raterusConnect With a Mentor Commented:
If your Header.ascx UserControl would expose a property Title, like TornadoV has suggested, you could also expose a property Title in your base page.  In the Render method, you just set the UserControl's Title to whatever they've set in the base page, if they've even set it.  I think this should work for you.

so...In base page render

     namespace.common.header Header = null;
     Header = (header)LoadControl(Request.ApplicationPath + "/common/header.ascx");
     Header.PageTitle = this.PageTitle;
     form.Controls.AddAt(0, Header);
0
 
sbennettsAuthor Commented:
Hi raterus, does that mean I'd have to set the title in my header / base page though?  I'd need to set it in the code for the individual page for it to work as expected...
0
 
raterusCommented:
If you have set it up properly in your base page, and all your individual pages inherit from this page, then all your individual pages will be able to configure this.
0
 
TornadoVCommented:
sbennetts, sorry for not getting back faster, we lost all the power due to a t-storm last night.

raterus is absolutely correct, all you need to do at this point is to chain things together and all your pages that inherit from your BASE page will be able to set their individual PageTitle.

0
 
sbennettsAuthor Commented:
OK, thanks guys, nightmare and not quite as the code above but all working.  Solution was:

In ASPX:
protected System.Web.UI.HtmlControls.HtmlGenericControl pageTitle;

private void Page_Load(object sender, System.EventArgs e)
{
    this.PageTitle = "Hello test";
}

Base page has:
if (Page.FindControl("pageTitle") != null && PageTitle != null)
      (Page.FindControl("pageTitle") as System.Web.UI.HtmlControls.HtmlGenericControl).InnerText = PageTitle;

(Added the if to only try it if it finds a server side control called pageTitle and / or I've set it in one of my pages)
                  
And now I no longer look for the control to add my header etc using a number but properly using a named control
System.Web.UI.Control form = Page.FindControl("bodypanel");

header.ascx user control simply added:
public string PageTitle;

Eventually, all working


Thanks very much guys, question is who gets the points? Split ok?
0
 
TornadoVCommented:
Sure, I'm glad it's working for you.
0
 
raterusCommented:
...gee I hate to open a can of worms, but what you have may work, but it's not really a good solution in my opinion.  Anytime you have to call Page.FindControl, you are recursively searching through every control in your page, and this is a lot of them, since somecontrols, like the DataGrid, are made up of tons of inner controls.  You can easily avoid this major performance hit by using properties in both your base page and usercontrol class.  
0
 
sbennettsAuthor Commented:
...and the answer is to use master pages, Page.Title, vs2005 and ASP2.0?
0
 
raterusCommented:
well ideally that is my first suggestion, but there is a better way to accomplish this even with 1.1.  I thought we were working toward that.

Do you understand what I'm saying when I say to have two properties, one in the base page class, and another in the usercontrol?
0
 
sbennettsAuthor Commented:
Um, not sure!  Possibly
0
 
sbennettsAuthor Commented:
Any more thoughts before I accept the answers and close raterus?

I'd rather do things the right way if you have anything more to add...

Thanks
Simon
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.