asp.net当修改header时提示:The Controls collection cannot be modified because the control contains code blocks (i.e. <% ... %>)

The Problem:

后台代码:

HtmlGenericControl jsGC = new HtmlGenericControl("script");
jsGC.Attributes.Add("type""text/javascript");
jsGC.Attributes.Add("src""http://www.cnblogs.com/js/Cookie.js");
Page.Header.Controls.Add(jsGC);

 提示错误:

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

The Solution:

A. 把Header中的带有<%=...% >这样的代码移出。

B. 把代码放入placeholder

C. (1). 把<%=..%>改为:<%# ...%>

    (2). 在page_load函数中增加:Page.Header.DataBind();

D. 动态生成。

 

参考资料:http://leedumond.com/blog/the-controls-collection-cannot-be-modified-because-the-control-contains-code-blocks/

 

 

 

 

 

posted @ 2010-03-30 11:24  空紫竹  阅读(429)  评论(0编辑  收藏  举报