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.