0

我有这个代码

 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. <% ... %>).

想法?

4

1 回答 1

1

我看不出错误消息中有什么不清楚的地方。您的<head>标签包含一个<% %>块,因此您不能在运行时在那里动态添加控件。

要解决此问题,请添加一个占位符并将您的元标记放在那里:

<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);  
}
于 2010-04-30T02:43:54.223 回答