[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Dynamically changing the title attribute asp.net

Posted on 2006-06-01
30
Medium Priority
?
527 Views
Last Modified: 2013-11-19
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
0
Comment
Question by:sbennetts
  • 14
  • 8
  • 7
  • +1
30 Comments
 
LVL 5

Expert Comment

by:Sam_Jayander
ID: 16810130
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
 
LVL 33

Expert Comment

by:raterus
ID: 16810180
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
 
LVL 11

Expert Comment

by:TornadoV
ID: 16810212
Or you can simply put this in your page_load event:

string myNewTitle = "Your new title";

Title = myNewTitle;
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 2

Author Comment

by:sbennetts
ID: 16810320
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
 
LVL 2

Author Comment

by:sbennetts
ID: 16810355
I'm using VS2003
0
 
LVL 33

Expert Comment

by:raterus
ID: 16810381
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
 
LVL 11

Expert Comment

by:TornadoV
ID: 16810406
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
 
LVL 2

Author Comment

by:sbennetts
ID: 16810419
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
 
LVL 11

Expert Comment

by:TornadoV
ID: 16810470
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
 
LVL 2

Author Comment

by:sbennetts
ID: 16810532
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
 
LVL 11

Accepted Solution

by:
TornadoV earned 900 total points
ID: 16810594
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
 
LVL 2

Author Comment

by:sbennetts
ID: 16810651
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
 
LVL 11

Expert Comment

by:TornadoV
ID: 16810768
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
 
LVL 2

Author Comment

by:sbennetts
ID: 16810891
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
 
LVL 2

Author Comment

by:sbennetts
ID: 16811127
There, taken both suggestions :-) how good am I?

Any thoughts?
0
 
LVL 33

Expert Comment

by:raterus
ID: 16811389
I guess upgrading to asp.net 2.0 is out of the question...  All these problems go away quickly when you use masterpages.
0
 
LVL 2

Author Comment

by:sbennetts
ID: 16811470
How easy is an upgrade?  

Is there any other way?
0
 
LVL 33

Expert Comment

by:raterus
ID: 16811502
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
 
LVL 2

Author Comment

by:sbennetts
ID: 16811566
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
 
LVL 33

Assisted Solution

by:raterus
raterus earned 900 total points
ID: 16816281
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
 
LVL 2

Author Comment

by:sbennetts
ID: 16816322
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
 
LVL 33

Expert Comment

by:raterus
ID: 16816530
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
 
LVL 11

Expert Comment

by:TornadoV
ID: 16816655
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
 
LVL 2

Author Comment

by:sbennetts
ID: 16816753
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
 
LVL 11

Expert Comment

by:TornadoV
ID: 16816790
Sure, I'm glad it's working for you.
0
 
LVL 33

Expert Comment

by:raterus
ID: 16816846
...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
 
LVL 2

Author Comment

by:sbennetts
ID: 16816882
...and the answer is to use master pages, Page.Title, vs2005 and ASP2.0?
0
 
LVL 33

Expert Comment

by:raterus
ID: 16816938
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
 
LVL 2

Author Comment

by:sbennetts
ID: 16816963
Um, not sure!  Possibly
0
 
LVL 2

Author Comment

by:sbennetts
ID: 16818207
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

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Shoutout to Emily Plummer (http://www.experts-exchange.com/members/eplummer26.html) for giving me this article! She did most of it, I just finished it up and posted it for her :)    Introduction In a previous article (http://www.experts-exchang…
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
Suggested Courses

829 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question