Register Login
   
 Categories  
 Archive  
 Authors  
 Articles  

Current Articles | Categories | Search | Syndication

Wednesday, June 08, 2005
Setting the Page Title Dynamically
By Scott McCulloch @ 4:34 PM :: 6241 Views :: 4 Comments :: Article Rating :: DotNetNuke
 
A DotNetNuke page contains many modules and set's its title (the label shown in your browser window) from the information about the current page. This information is usually the page title (or page name if no title specified).

Sometimes it's useful however to dynamically set this value from within a module, a good example is my news articles module which dynamically set's the page title based on the title of the article. Another example is the latest version of Active Forums.

To change the title is relatively simple, each module is a user control and all user controls belong to a page (so we should be able to get a reference to that page from our module/user control).

The base page for DotNetNuke is a special class within DotNetNuke (CDefault) and has additional properties not found in the traditional page class (System.Web.UI.Page). These additional properties contain a property for title.

So the first step I do is to create a property representing a reference to this class:-

Public ReadOnly Property BasePage() As DotNetNuke.Framework.CDefault
     Get
          Return CType(Me.Page, DotNetNuke.Framework.CDefault)
     End Get
End Property

The above bit of code exposes a property that casts the module's page reference to the correct class. (I think that maybe this property should belong in PortalModuleBase so doing the above step would be unnecessary).

Now the easy part, from our module we can now do:-

Me.BasePage.Title = "My Custom Title"

That's it! There are quite a few other properties in the CDefault class you can take advantage of, for my news articles module, I was able to add additional CSS references for my method templating.

Rating
Comments
By Anonymous User @ Thursday, June 23, 2005 7:45 AM
Being able access the page properties leads to some other requirements that I ran into. One is how do I show the last-updated date and time on the page. Keep in mind that if a record in one of the modules on that page was the most recently modified record, then the modified date on that record should be shown on the page as a last-updated label tag.

ByJohn @ Saturday, October 07, 2006 10:46 PM
Just want to say that this still works in the latest DNN4.3.5 version. Also you can set the description in this way.
Thanks Scott.

By Bruce Chapman @ Wednesday, July 25, 2007 2:05 AM
You can also use the for the page META tags and Description. Just set the BasePage.Description property and (append) to the BasePage.Keywords property.

I really enjoy finding the correct solution - some of the other solutions proposed are medieval in their brutality - doing regex replacement on the page render.

thx Scott

By patrick smith @ Tuesday, March 25, 2008 8:56 PM
So how would i put the article title in the title? I don't know how to access the database field

Click here to post a comment