2010-04-30 135 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