Make the page title runat the server in ASP.NET#
In ASP.NET you have a (i.e.: exactly one) html form in which all your controls reside. If you want serverside access to any control, you just give the appropriate tag an ID and the runat="server" attribute. If the designer didn't already do this for you, declare an object of the proper type in your codebehind page with the exact same name as the ID you gave your tag and you're done - you now have full control over your uhm... control. You can do this with any tag - if there is no serverside equivalent for what you're trying to do, just use the HtmlGenericControl.

But what happens if you go outside the bounds of your form? A typical problem I've seen posted a few times is: "How can I set the title of my webpage in code?". This implies accessing the <title> tag in your html, which is in the <head> section (i.e. before the actual <body> and <form>). It turns out you can just as well make any tag there run at the server. So you might go:

<title id="lblTitle" runat="server">WebPage1<title>

In your codebehind you declare your object:

Protected WithEvents lblTitle As HtmlGenericControl

And voila, set the text as follows: lblTitle.InnerText = "This ran at the server!".

Now we can take this one step further. Why not create something that encapsulates a portion of the <head> tag? Just add a User Control to your project, e.g. "HtmlHead.ascx" and make it emit some header tags such as title, meta tags, link tags to import css files, ... anything you can think of really. If you want programmatic control over any of these tags, give them an ID and make them runat=server so you have them at your disposal in your codebehind class. Now you can declare nice userfriendly properties and methods on your User Control such as PageTitle, AddCssFile, etc.

In any webpage, just can now register the user control and add it to your <head> tag as follows:

<%@ Register TagPrefix="uc1" TagName="HtmlHead" Src ="HtmlHead.ascx" %>

Put an instance of this control in the <head>:

<head>
   <uc1:htmlhead id="uctHtmlHead" runat="server"></uc1:htmlhead>
   (...other tags, such as the ones generated by Visual Studio go here...)
</head>

Don't forget to declare the object in your codebehind:

Protected WithEvents uctHtmlHead As HtmlHead

And that's it - you're done: full control over your html head!

uctHtmlHead.PageTitle = "ASP.NET rocks :-)"
Comments are closed.
All content © 2012, Jelle Druyts
On this page

Recent Photos
www.flickr.com
This is a Flickr badge showing public photos from Jelle Druyts. Make your own badge here.
Advertising
Top Picks
Statistics
Total Posts: 350
This Year: 0
This Month: 0
This Week: 0
Comments: 530
Archives
Sitemap
Disclaimer
This is my personal website, not my boss', not my mother's, and certainly not the pope's. My personal opinions may be irrelevant, inaccurate, boring or even plain wrong, I'm sorry if that makes you feel uncomfortable. But then again, you don't have to read them, I just hope you'll find something interesting here now and then. I'll certainly do my best. But if you don't like it, go read the pope's blog. I'm sure it's fascinating.

Powered by:
newtelligence dasBlog 2.0.7226.0

Sign In