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 に答える