views:

50

answers:

1

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")
        );
}
+2  A: 

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 */
}
y34h
I'll start digging though all my code again. I haven't found any in the last couple times i've looked though. But if i cant use controls and use code like the above, how am I supposed to get this stuff to work? The more I use ASP more more I hate it <-- My opinion.
Justin808
I converted my script to what you have above. This solved the issues.
Justin808