Webforms元标记错误?

时间:2014-02-04 14:23:16

标签: c# asp.net webforms

<meta name="keywords" content='<asp:Literal ID="litMetaKeywords" runat="server" />'/>
<meta name="description" content='<asp:Literal ID="litMetaDescription" runat="server" />'/>

我在我正在处理的Web应用程序中找到了上述代码。我收到以下错误

The name 'litMetaDescription' does not exist in the current context

litMetaKeywords也一样。如果asp:literal不在此元标记中,则代码将起作用。什么是元标记?它的用途是什么?这是导致我的错误的原因吗?

1 个答案:

答案 0 :(得分:2)

<asp:Literal>标记放在content属性中将无法从后面的代码中访问它。您可以使用受保护变量和内联代码的组合。

代码背后:

protected string MetaKeywords;
protected string MetaDescription;

protected void Page_Load(object sender, EventArgs e)
{
    MetaKeywords = "values for meta keywords";
    MetaDescription = "values for meta description";
}

ASPX:

<meta name="keywords" content='<%= MetaKeywords %>'/>
<meta name="description" content='<%= MetaDescription %>'/>

有关HTML元标记的详细信息,请参阅here