I would like to set meta tag value of a page at runtime and to do the same I put a literal control and then try to set the text value of the literal control as shown below:
<meta name="description" content="<asp:literal id="pgMetaDescription" runat="server" />"
<script runat="server">
Sub Page_Load
pgMetaDescription.text = "Test Page"
End Sub
</script>
However, when i try to use literal control in a meta tag I get the following error:
The base class includes the field 'pgMetaDescription', but its type (System.Web.UI.WebControls.Literal) is not compatible with the type of control (System.Web.UI.HtmlControls.HtmlGenericControl).
I do not understand; the meta tag is not a server control since it I have not used any runat property for the tag, then why it is treating it as a server control?
Any help please...
Thanks,
HimanshuDeclare a Global variable and just contruct the whole thing as a plain string:
<script runat="server"
Protected myMeta As StringSub Page_Load
myMeta = "<meta http-equiv=""refresh"" content=""15"">"
End Sub
</script>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<title>Polisen ::: Kösystem</title>
<meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
<%= strMeta %> ...
I am surprised adec showed you the quick and dirty way. If you don't want it there, then set mTag.Visible=False
<script runat="server">
Private Sub Page_Load ( ByVal sender As Object, ByVal e As EventArgs )
mTag.Attributes("content") = "15"
mTag.Attributes("http-equiv") = "refresh"
' mTag.Visible = False
End Sub
</script>
<!--
..
-->
<meta id="mTag" runat="server" />
Hi,
Thankyou guys for showing me the way.
Actually I had thought of using a generic control for meta tag as suggested by pickyh3d.
However, it did not use it because that will add </meta> in the html code and I do not know if this will affect the way the page is indexed by search engines, because I have not seen the closing tag for meta in any html document.
At present I have found a workaround in my method of having literal controls BUT enclosed in single quote and not double quote, like this:
<meta name="description" content='<asp:literal id="pgDesc" runat="server" />' >
Now, i do not get any error!!!
What are your views on this?
I am still puzzled and to why in the first place it treats meta tag as a server control and gives error?
Cheers!
Himanshu
Just so you know, Google does not use the META tags.
In my view these ways are all wrong! You what the Meta tags to render in the header, right? so the best way is as follows:
1. Create your Meta tag as normal, in the HEAD tag of your .aspx page and then add an id and runat=server attribute to the Meta tag. IMPORTANT - You must add a closing Meta tag for this to work of you will get a pharse error
Example:
<HEAD>
<META id=metaLang runat=server content=C# name=CODE_LANGUAGE></META>
</HEAD>
2. Now in your code file (.aspx.cs or .aspx.vb) you declare "metaLang" as a HtmlGenericControl
Example*:
protected System.Web.UI.HtmlControls.HtmlGenericControl metaLang;
* Code is in C#
3. Now you can access the META tag attributes.
Example*:
metaLang.Attributes["content"] = "VB";
This can be used for any head tag. If you want to change the values between opening and closing tags, you would use "InnerText"
Example for a "Title" tag:
titleTag.InnerText = "My Home Page";
I hope this helps.
This method can be applied to any tags on a page as long as the control is declared correctly eg. an anchor tag must be declared as "HtmlAnchor" and not a "HtmlGenericControl"
Regards
Rog. (MCP)
Thanks for your views.
I agree that the methodology of using literal controls is wrong. Actually, it fails when the value contains a single quote.
So I am using the methodology of treating the meta tag as a server control with attributes as suggested and I guess even though I do not achive pure meta tag html syntax (with extras of id and closing meta tag), it is fine.
Himanshu
0 comments:
Post a Comment