Wrox Programmer Forums
|
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 software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old December 14th, 2003, 08:23 PM
Authorized User
 
Join Date: Jul 2003
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.

 
Old December 15th, 2003, 11:32 AM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
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.
 
Old December 15th, 2003, 01:56 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,110
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.

 
Old June 16th, 2004, 04:26 PM
Registered User
 
Join Date: Jun 2004
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!


 
Old June 28th, 2005, 12:57 PM
Registered User
 
Join Date: Jun 2005
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


 
Old October 11th, 2005, 08:48 AM
Registered User
 
Join Date: Oct 2005
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>");






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 08: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





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.