Tuesday, March 13, 2012

Setting title tag programmatically

How can I set the page <title></title> programmatically using asp.net?

I can't put the <head></head> section with asp.net form tags, can I??

Thanks

accelerator

Hello, you can do this:
<title id="PageTitle" runat="server" /></title>
From code behind, declare a variable as follows:
protected System.Web.UI.HtmlControls.HtmlGenericControl PageTitle;
PageTitle.InnerText = "Hello World";
regards

That's pretty cool. Where would this variable declaration reside, exactly?
Put that variable before the Page_Load method.
regards

ASP.NET best practice states:

Use server controls sparingly and appropriately: Even though it is extremely easy to use, a server control might not always be the best choice. In many cases, a simple rendering or databinding substitution will accomplish the same thing. [Source]

Therefore I suggest simply declaring a string variable and use it in a Code Render Block:

<title><%=var %></title>

0 comments:

Post a Comment