views:

183

answers:

2

I have this code

 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);  
}

And it's returning this error (on a content page)

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

Thoughts?

A: 

I don't see what's not clear from the error message. Your <head> tag contains a <% %> block, and therefore you cannot dynamically add controls there at runtime.

To resolve this, add a placeholder and put your meta tags there:

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

And then:

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);  
}
Fyodor Soikin
ahh, thanks I knew the issue I just didn't know how to resolve it. Thanks again!
Bry4n