c# ASP.NET Controls 集合代码块废话

发布于 2024-09-26 18:26:27 字数 1930 浏览 4 评论 0原文

可能导致什么:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夏花。依旧 2024-10-03 18:26:27

也许您有一些包含 <% %> 的自定义控件,例如 ajaxcontroltoolkit 日历。我曾经遇到过同样的问题,唯一的解决方案是删除该控件。

尝试删除/注释代码片段,以便您可以准确找到导致问题的原因。

编辑:不确定这是否可以解决问题,但这是注入js的正确方法:

public static void addJavascriptIDs(Page page, params Control[] ctls)
{
    string js = "";
    foreach (Control ctl in ctls)
        js += string.Format("var ASP_{0} = '{1}';", ctl.ID, ctl.ClientID);
    page.ClientScript.RegisterClientScriptBlock(typeof(object), "IDs", js, true)
    /* http://msdn.microsoft.com/en-us/library/bahh2fef.aspx */
}
public static void addJavascriptIncludes(Page page, params string[] scripts)
{
    foreach (string script in scripts)
        page.ClientScript.RegisterClientScriptInclude(typeof(object), script, page.ResolveUrl(script));
    /* http://msdn.microsoft.com/en-us/library/kx145dw2.aspx */
}

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:

public static void addJavascriptIDs(Page page, params Control[] ctls)
{
    string js = "";
    foreach (Control ctl in ctls)
        js += string.Format("var ASP_{0} = '{1}';", ctl.ID, ctl.ClientID);
    page.ClientScript.RegisterClientScriptBlock(typeof(object), "IDs", js, true)
    /* http://msdn.microsoft.com/en-us/library/bahh2fef.aspx */
}
public static void addJavascriptIncludes(Page page, params string[] scripts)
{
    foreach (string script in scripts)
        page.ClientScript.RegisterClientScriptInclude(typeof(object), script, page.ResolveUrl(script));
    /* http://msdn.microsoft.com/en-us/library/kx145dw2.aspx */
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文