I’m using webforms ASP.NET, with masterpages. I want to add a LANG attribute to the <title> tag. ASP.NET automatically generates a title tag. I’ve tried adding my own tag with an ID and runat=”server”, like this:
<title id="titleBlock" runat="server"></title>
When doing this, I can set an attribute like the following without any errors.
titleBlock.Attributes.Add("lang", "it");
However, ASP.NET wipes out my <title> tag completely and puts its own in without my LANG attribute. Is there any way to accomplish this? Thanks very much.
This happens because the HtmlTitle control doesn’t provide a RenderAttributes implementation. You can’t (easily) subclass the control in this case but there’s another option. The power of Control Adapters isn’t limited to WebControls – the concept also extends to HtmlControls as well.
Drop the following in a *.browser file under the App_Browsers directory of your site:
Here’s a prototype for the corresponding adapter:
Note that the internal render implementation of HtmlTitle differs somewhat:
I’m not sure when a title would have child elements, so I don’t think this an issue.
Hope this helps.
As an aside, it would be much easier to add this attribute on the client using jQuery.