Setting the page title dynamically in ASP.NET
With a database driven site it’s very useful if pages giving details of a specific product have that product name in the page title.
This sounds like it should be quite easy but it isn’t. You can insert data from a field into a page quite easily by binding the data to a control. However, the ASP.NET controls have to sit with the <form> tags in the <body> of the HTML so binding data to the <head> section is more difficult.
It has to be done programmatically. Here how’s it’s done in VB.
dv = CType(srcDataSourceControl.Select(DataSourceSelectArguments.Empty), Data.DataView)
Dim dr As Data.DataRowView = dv(0)
Dim strProductName As String = CStr(dr(“ProductName”))
Dim strProductNumber As String = CStr(dr(“ProductNumber”))
Dim strProductCategory As String = CStr(dr(“ProductCategory”))
Page.Header.Title = strProductName & ” – ” & strProductNumber & ” – ” & strProductCategory
This example uses srcDataSourceControl as the data source control and assumes that the database table has fields called ProductName, ProductNumber and ProductCategory. You can bind as many fields as you like in this way.
The title is set by giving a string value to Page.Header.Title. This can be strings bound with DataView (strProductName) or bits of text in quote marks (” – “) joined by the & sign.
One final important thing. The <head> section in your page HTML must include the runat=”server” to enable the header content to be manipulated on the server side.
| Print article | This entry was posted by admin on March 12, 2009 at 3:46 pm, and is filed under ASP.NET, DataRowView, DataView, MasterPage, Page.Header.Title, VB.NET. Follow any responses to this post through RSS 2.0. You can leave a response or trackback from your own site. |


about 1 year ago
Steven,
I’m not too clear on your suggestion. Could you provide more details or illustration?
Thanks in advance.
about 1 year ago
Its even easier if you are in a master page, as on the content page, you just have to set the Page.Title = X. For dynamic content, and if your loading the page from a SQL dataset, its easy to pull while the page is loading
IF Not Page.IsPostBack Then
‘Your Code for SQL info
Page.Title = sqldatareader(“productnumber”).ToString
‘Other code for the statement…
End If