Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 1.0 and 1.1 > ASP.NET 1.0 and 1.1 Professional
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
ASP.NET 1.0 and 1.1 Professional For advanced ASP.NET 1.x coders. Beginning-level questions will be redirected to other forums. NOT for "classic" ASP 3 or the newer ASP.NET 2.0 and 3.5
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 1.0 and 1.1 Professional section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developersí questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old December 14th, 2003, 07:23 PM
Authorized User
 
Join Date: Jul 2003
Location: , , .
Posts: 98
Thanks: 0
Thanked 0 Times in 0 Posts
Default Changing Page Title From User Control

Hello Everyone and thanks for your help in advance. I have a web site that is comprised of one web from, Default.aspx and multiple user controls that are loaded dynamically to alter the content of the page. I am trying to programmatically change the page title, however, it must be done from within the user controls. For example, when the control Details.ascx is loaded, I want the page tile to read something like "Contact Detail for John Doe". Obviously, the user control will have to pass the information back to the main page. I am not exactly sure how to do this. Any help would be greatly appreciated. Thanks.

Reply With Quote
  #2 (permalink)  
Old December 15th, 2003, 10:32 AM
planoie's Avatar
Friend of Wrox
Points: 16,481, Level: 55
Points: 16,481, Level: 55 Points: 16,481, Level: 55 Points: 16,481, Level: 55
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2003
Location: Clifton Park, New York, USA.
Posts: 5,413
Thanks: 0
Thanked 16 Times in 16 Posts
Default

All web controls contain the Page property. It references the containing page of the control. With it you can access anything on the instance of that page class.

- Modify your page's <title> tag to include runat="server" and an ID attribute (for example id="tagTitle"). This tag will be instantiated as an HtmlGenericControl.
- The <title> will also need an associate class field in the code behind for the page:

Protected tagTitle As System.Web.UI.HtmlControls.HtmlGenericControl

- Add a public property to your page class to expose the title tag's property that you need. We'll name it "PageTitle":

Public Property PageTitle() As String
    Get
        Return tagTitle.InnerHtml
    End Get
    Set(ByVal Value As String)
        tagTitle.InnerHtml = Value
    End Set
End Property

- Within your user control, you'll need to convert the Page property of the user control to the right page class in order to see the property you added. Here's the code you call within the user control:

CType(Me.Page, default).PageTitle = "My customized page title"

Important points:
- default is the class name of the ASPX. You may need to set this based on the actual class associated with the ASPX you use this on.
- If you do this on multiple user controls, the LAST one to make the call will get the final say about the page title.

Here is a section of an article that explains a slightly more complex concept that greatly simplifies this kind of operation when you have a situation that requires this type of coding on lots of pages and controls. It has some sample code that you can adapt to suit your specific need.

Peter
------------------------------------------------------
Work smarter, not harder.
Reply With Quote
  #3 (permalink)  
Old December 15th, 2003, 12:56 PM
Friend of Wrox
 
Join Date: Jun 2003
Location: , , USA.
Posts: 1,111
Thanks: 0
Thanked 3 Times in 3 Posts
Default

You can try this. Declare this in your Header User Control:
Public PageTitle as String = "My Title"

<html><head><title><%=PageTitle%></title></head>

Then on your default.aspx page use this as the Header User Control:
<MyCompanyUserCtrl:Header id="ctrlHeader" PageTitle="The Home Page" runat="server" />

*Not tested.

Reply With Quote
  #4 (permalink)  
Old June 16th, 2004, 04:26 PM
Registered User
 
Join Date: Jun 2004
Location: , , .
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

including the include runat="server, works fine until you start modifying the design Visual Studio 2001/2003, removes the runat="server attribute for some reason.

Has anyone else experienced this
This is not the best solution unfortunately.

I tried following this article
http://www.dotnetjunkies.com/HowTo/4...CFD2CBF96.dcik

but I'm unable to do anything from the code behind. Such a pain just to changethe ricken page title!


Reply With Quote
  #5 (permalink)  
Old June 28th, 2005, 12:57 PM
Registered User
 
Join Date: Jun 2005
Location: , , .
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

May be a little late, but here's how I do it.

In your single web page, write this:
<title>
  <asp:Literal runat="server" id="TitleTag"></asp:Literal>
</title>

In your user control, do this:
Dim titleTag As Literal = Me.Page.FindControl("TitleTag")
If Not titleTag Is Nothing Then
   titleTag.Text = "New page title"
End If


Reply With Quote
  #6 (permalink)  
Old October 11th, 2005, 08:48 AM
Registered User
 
Join Date: Oct 2005
Location: Lakewood, OH, USA.
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Put this in the Page_Load event of the user control

Response.Write("<title>My Title</title>");

Reply With Quote
Reply


Thread Tools
Display Modes

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 Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Master Page - User Control communication Aaron Edwards ASP.NET 2.0 Basics 8 March 7th, 2008 07:52 AM
User Control not rendering in Master Page skmcusp ASP.NET 2.0 Basics 2 September 10th, 2007 04:58 PM
how to reference user control in master page.. gbianchi ASP.NET 2.0 Basics 1 May 1st, 2006 09:26 AM
Changing Crystal Report Title from VB Code Niaz Pro VB Databases 4 June 9th, 2004 09:51 PM
User control caching on a single page mkerchenski ASP.NET 1.0 and 1.1 Basics 1 June 9th, 2004 09:54 AM



All times are GMT -4. The time now is 11:48 PM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.