This is a discussion on How to Dynamically Setting the Page's Title in ASP.NET 2.0? within the C# Programming forums, part of the Software Development category; How to Dynamically Setting the Page's Title in ASP.NET 2.0?...
| |||||||
| Register | FAQ | Members List | Calendar | Mark Forums Read |
| |||
| One of the many new features found in ASP.NET 2.0 is the programmatically-accessible <head> region, which can be added to an ASP.NET page using the following markup: <head runat="server"> <title>Untitled Page</title> ... other <head>-level elements ... </head> The above markup (less the"... other <head>-level elements ..." part) is what Visual Studio adds to a new ASP.NET page or master page, by default. Note the runat="server". When an ASP.NET page is requested, the ASP.NET engine parses the HTML portion and creates the page's control hierarchy. During this process, the markup that maps to server-side controls is converted into an appropriate object instance. For example, during this process the TextBox Web control's markup is parsed (<asp:TextBox runat="server" id="myTextBox" ... />) and a TextBox class instance is created to participate during the events of the page's lifecycle. The runat="server" portion is what instructs the ASP.NET engine that a particular piece of markup is a server-side control versus static HTML. The <head runat="server"> markup is instantiated as an HtmlHead class instance, which has properties mapping to <head>-level settings, including: • Title - the page's title • Style - a collection of cascading style sheet (CSS) entries defined for the page In order to be able to programmatically access these <head>-level settings, you need to be certain to add the <head runat="server"> to your ASP.NET page or master page, if it's not present already. Programmatically Working with the <head> Region Assuming you have a programmatically-accessible <head> region defined in your page or the page's master page, you can programmatically access it using the Page class's Header property. For example, to programmatically set the title of a page, add the following line of code to the page's Page_Load event handler: Page.Header.Title = "The current time is: " & DateTime.Now.ToString() Alternatively, you can use the Page.Title property as a shortcut to Page.Header.Title. Furthermore, if you are using master pages, this code can work, as written, from either the master page or the ASP.NET page that uses the master page. In such a scenario, the <head> region should be defined in the master page, but the ASP.NET page can still access it via Page.Header. |
![]() |
| Thread Tools | |
| Display Modes | |
| |
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| how can i disable the title bar in JInternalFrame | saravanan | Java Programming | 7 | 03-28-2008 06:33 AM |
| How do I change another window's title, that is, the content of title bar at the top | itbarota | HTML, CSS and Javascript Coding Techniques | 1 | 09-13-2007 03:02 AM |
| How to give bug title & bug description fo ODD division? | devarajan.v | Quality Engineering and Methodologies | 1 | 07-27-2007 12:14 AM |
| Title Tag Optimization Tips | vadivelanvaidyanathan | Search Engine Optimization | 0 | 03-30-2007 02:58 AM |
| SEO Optimization - Page Title | drecko | Search Engine Optimization | 2 | 02-19-2007 01:28 AM |