2010-04-30 93 views
0

我有这样的代码C#和添加动态META标签

protected void Page_Load(object sender, EventArgs e) 
{ 
    DataSet.DestinationsDataTable GetDestinations = (DataSet.DestinationsDataTable)dta.GetData(); 
    Page.Title = GetDestinations.Rows[0]["Meta_Title"].ToString(); 

    HtmlMeta hm = new HtmlMeta(); 
    HtmlHead head = (HtmlHead)Page.Header; 
    hm.Name = GetDestinations.Rows[0]["Meta_Desc"].ToString(); 
    hm.Content = GetDestinations.Rows[0]["Meta_Key"].ToString(); 
    head.Controls.Add(hm); 
} 

而且它返回此错误(在内容网页上)

The Controls collection cannot be modified because the control contains code blocks (i.e. <% ... %>). 

的思考?

回答

1

我看不出错误信息中的内容。 您的<head>标记包含一个<% %>块,因此您无法在运行时动态添加控件。

要解决这个问题,添加一个占位符,并把你的meta标签有:

<html> 
    <head> 
     ... 
     <asp:PlaceHolder runat="server" ID="metaTags" /> 
    </head> 
... 

然后:

protected void Page_Load(object sender, EventArgs e) 
{ 
    DataSet.DestinationsDataTable GetDestinations = (DataSet.DestinationsDataTable)dta.GetData(); 
    Page.Title = GetDestinations.Rows[0]["Meta_Title"].ToString(); 

    HtmlMeta hm = new HtmlMeta(); 
    hm.Name = GetDestinations.Rows[0]["Meta_Desc"].ToString(); 
    hm.Content = GetDestinations.Rows[0]["Meta_Key"].ToString(); 
    this.metaTags.Controls.Add(hm); 
} 
+0

啊,谢谢,我知道这个问题我只是不知道如何解决它。再次感谢! – balexander 2010-04-30 02:47:05