c# ASP.NET Controls 集合代码块废话
可能导致什么:
The Controls collection cannot be modified because the control contains code blocks (i.e. <% ... %>).
我问是因为,代码不包含代码块。任何地方。没有一个<%= %>;阻止整个站点的任何页面。我所拥有的只是<%@ %>指示线。
编辑:
下面是导致错误的代码。
/// <summary>
/// Adds javascript variables to a page so that ASP control ID are accessable via Javascript. The
/// variable names are ASP_ControlID.
/// </summary>
/// <param name="page">The Page to add the javascript to</param>
/// <param name="ctls">The controls to make accessable</param>
public static void addJavascriptIDs(Page page, params Control[] ctls)
{
Literal litJS = new Literal();
litJS.Text = getJavascriptIDs(ctls);
page.Form.Controls.Add(litJS); ////////// <-- Error Here
}
/// <summary>
/// Returns a string containing the javascript to allow javascript to access
/// ASP controls.
/// </summary>
/// <param name="ctls">The HTML and javascript to create the javascript variables</param>
/// <returns>The script</returns>
public static string getJavascriptIDs(params Control[] ctls)
{
string js = "\n<script type=\"text/javascript\">\n";
foreach (Control ctl in ctls)
js += "\tvar ASP_" + ctl.ID + " = '" + ctl.ClientID + "';\n";
js += "</script>\n\n";
return js;
}
这是 Page_Load 函数:
protected void Page_Load(object sender, EventArgs e)
{
formHowHear.Attributes.Add("onChange", "displayOther(this);");
LoadValues();
if (!IsPostBack)
{
LoadDefaults();
BindValidation();
}
else
{
Submitted();
}
CMSUtil.addJavascriptIDs(this, formEmail1, formEmail2, formUsername);
CMSUtil.addJavascriptIncludes(Page,
ResolveUrl("~/js/jquery-1.4.2.min.js"),
ResolveUrl("~/admin/js/TVCMS.js")
);
}
What can cause:
The Controls collection cannot be modified because the control contains code blocks (i.e. <% ... %>).
I ask because, the code does not contain code blocks. Anywhere. There is not a single <%= %> block on any page in the entire site. All i have are the <%@ %> directive lines.
Edit:
Below is the code causing the error.
/// <summary>
/// Adds javascript variables to a page so that ASP control ID are accessable via Javascript. The
/// variable names are ASP_ControlID.
/// </summary>
/// <param name="page">The Page to add the javascript to</param>
/// <param name="ctls">The controls to make accessable</param>
public static void addJavascriptIDs(Page page, params Control[] ctls)
{
Literal litJS = new Literal();
litJS.Text = getJavascriptIDs(ctls);
page.Form.Controls.Add(litJS); ////////// <-- Error Here
}
/// <summary>
/// Returns a string containing the javascript to allow javascript to access
/// ASP controls.
/// </summary>
/// <param name="ctls">The HTML and javascript to create the javascript variables</param>
/// <returns>The script</returns>
public static string getJavascriptIDs(params Control[] ctls)
{
string js = "\n<script type=\"text/javascript\">\n";
foreach (Control ctl in ctls)
js += "\tvar ASP_" + ctl.ID + " = '" + ctl.ClientID + "';\n";
js += "</script>\n\n";
return js;
}
This is the Page_Load function:
protected void Page_Load(object sender, EventArgs e)
{
formHowHear.Attributes.Add("onChange", "displayOther(this);");
LoadValues();
if (!IsPostBack)
{
LoadDefaults();
BindValidation();
}
else
{
Submitted();
}
CMSUtil.addJavascriptIDs(this, formEmail1, formEmail2, formUsername);
CMSUtil.addJavascriptIncludes(Page,
ResolveUrl("~/js/jquery-1.4.2.min.js"),
ResolveUrl("~/admin/js/TVCMS.js")
);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
也许您有一些包含
<% %>
的自定义控件,例如 ajaxcontroltoolkit 日历。我曾经遇到过同样的问题,唯一的解决方案是删除该控件。尝试删除/注释代码片段,以便您可以准确找到导致问题的原因。
编辑:不确定这是否可以解决问题,但这是注入js的正确方法:
maybe you have some custom control that contains
<% %>
, for example ajaxcontroltoolkit calendar. i had that same problem once, and the only solution was to remove that control.try removing/commenting pieces of code so you can locate exactly what causes the problem.
EDIT: not sure if this corrects the problem, but this is the correct way to inject js: