A friend told me that it is possible to change the header of a html web page using the vb .net code. Is this true? If so how do you do it?

Dani AI

Generated

A few practical notes that build on 's discovery (making the <title> a server control and updating it in code-behind) and the followups from and .

For simple title changes there is an even cleaner option: set the ASP.NET page title from code-behind via the Page.Title property instead of turning the <title> element into a server control. For meta tags, use the HtmlMeta class and add it to Page.Header so the head stays tidy and easy to manage. Example patterns (VB.NET) that avoid embedding title/meta tags inside a form:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
    Page.Title = "Dynamic page title"
End Sub
Dim m As New System.Web.UI.HtmlControls.HtmlMeta()
m.Name = "keywords"
m.Content = "vb.net, asp.net, dynamic"
Page.Header.Controls.Add(m)

Troubleshooting tips and cautions: the disappearing runat="server" is most often caused by the Visual Studio HTML designer reformatting the file (as noted), not by the tags needing to be inside the form. Keep all head-related elements inside the <head> element; if server-side access to the head is needed, ensure the head has runat="server" or rely on Page.Header. When using Master Pages, put the runat server head in the master and provide a head ContentPlaceHolder for per-page title/meta updates.

Final notes: meta keywords are of limited SEO value today, but meta description and a clear title still matter for search snippets. Prefer manipulating Page.Title or Page.Header programmatically rather than relying on the designer to edit runat attributes.

Recommended Answers

All 8 Replies

Well I worked it out, here is how it's done.


The following is the html code.

<TITLE [b]ID=pageTitle RUNAT=server[/b]> </TITLE>

Then the vb .net code:

Public Class MyPage 
Inherits System.Web.UI.Page 
[b]Protected pageTitle As System.Web.UI.HtmlControls.HtmlGenericControl[/b]
Private Sub Page_Load(ByVal sender As System.Object, _ByVal e As System.EventArgs) Handles MyBase.Load 
[b]pageTitle.InnerText = "Dynamic page Title"[/b] 
End Sub
End Class

Anyways, just thought I'd share my findings with you guys.

Slade

Nice work Slade, I may actually put that to some use!

commented: Thanks :) Glad I could help +36

:D thanks Paladine, glad I could help!

commented: Sweet code to note! +36

I have been working with this code for a while now... after a few builds there is an error. What actually happens is in the aspx code, the "runat = server" disappears. This is more than likely because it isn't included in the <forms>tag</forms>


Anyways, just a little bit of extra info.

Slade

No, actually it's related to Visual Studio .NET's web page designer. When you are on the WYSIWYG ("Design") view of the page, it reformats your code, and sometimes screws up on tags. There is a setting to control what view you get when you double click on a file in the project window; I have it going to HTML View, which there is no problem with (I rarely use the designer for Web Development).

can you do the name with the keyword metatag?

Yup. It works for every tag :).

No, actually it's related to Visual Studio .NET's web page designer. When you are on the WYSIWYG ("Design") view of the page, it reformats your code, and sometimes screws up on tags. There is a setting to control what view you get when you double click on a file in the project window; I have it going to HTML View, which there is no problem with (I rarely use the designer for Web Development).

Lol I don't use it either, it's such a pain. I usually just build my project and view it in my browsers every now and then to make sure it's all ok :).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.