我有这段代码
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. <% ... %>).
思想?
答案 0 :(得分: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);
}