The Controls collection cannot be modified because the control contains code blocks

Posted by: jcasal on 8 September 2017, 9:28 am EST

  • Posted 8 September 2017, 9:28 am EST

    When I use the Print button on my Spread I get this error right on the printed page:


    <FONT color=#ff0000>The Controls collection cannot be modified because the control contains code blocks (i.e. <% ... %>).</FONT>


    This is the stack trace:


    [HttpException (0x80004005): The Controls collection cannot be modified because the control
    System.Web.UI.ControlCollection.RemoveAt(Int32 index) +2057591 System.Web.UI.ControlCollection.Clear() +25 FarPoint.Web.Spread.FpSpread.OnPrint() +204 FarPoint.Web.Spread.FpSpread.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String
    System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument)
    System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +174 System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)


    This only happens in my current solution. If I start a new project with a simple databound spread, it works.


    Does anybody know why I keep getting this error?


    Regards


    Julio

  • Replied 8 September 2017, 9:28 am EST

    Julio,


    Spread is creating a new page with just the Spread control for prrinting, so it sounds like there is a problem with that code removing some control on your page. What type of controls do you have on your page? Could you send us a small zipped project reproducing this problem for us to debug?

  • Replied 8 September 2017, 9:28 am EST

    Julio,


    We need to find out what combination of controls is causing this error. Could you try a small project to reproduce this issue. I would suggest creating a project where you have a Spread on a page that is in a tab control to see if that small project reproduces the same error.

  • Replied 8 September 2017, 9:28 am EST

    Well, I have just done that test with a tab control, and the problem does not appear. Question: If at runtime I take the spread control, save it into a session variable, redirect to an empty aspx page and, in that page I add the saved spread control to the page controls collection, should it work? I think that should work, as that would be a completely empty page. But, I have just tried that and I get the same error, but this time when loading the page. So I'm starting to wonder if the problem is in my particular configuration of the spread control, rather than on a combination with other controls on the page. Can that be possible?


    Thanks,


    Julio

  • Replied 8 September 2017, 9:28 am EST

    Thanks Scott. Well, I have lots of different controls on my page, like labels, buttons, dropdownlists, validators and image controls. I am also using third party commercial controls in this page, like web tabs and numeric editors. Actually, my page is inside another control, a tab control.


    Is there any way I could make this work? I have already sent the support request to Farpoint.


    Regards,


    Julio

  • Replied 8 September 2017, 9:28 am EST

    Julio,


    You can not add the Spread directly to the Controls collection of the page. How are you adding the stored Spread on the new page? You would need to add the Spread to the Controls collection of the HTMLForm.

     
    <SPAN class=kwd>Dim</SPAN> ss <SPAN class=kwd>As New</SPAN> FarPoint.Web.Spread.FpSpread
    ss.ID = <SPAN class=st>"testSpread"</SPAN>
    <SPAN class=kwd>For Each</SPAN> c <SPAN class=kwd>As</SPAN> Control <SPAN class=kwd>In</SPAN> Controls
    <SPAN class=kwd>If TypeOf</SPAN> c <SPAN class=kwd>Is</SPAN> HtmlForm <SPAN class=kwd>Then</SPAN>
    c.Controls.Add(ss)
    <SPAN class=kwd>End If
    Next</SPAN>
     
  • Replied 8 September 2017, 9:28 am EST

    Well yes, that is pretty much what I'm doing. I named my HTML form "PrintSpreadForm". So I do this:


    Current Spread Page:

    <FONT size=2>

    Session[</FONT><FONT color=#800000 size=2>"MySpread"</FONT><FONT size=2>] = fpointDtaObservations;


    Response.Redirect(</FONT><FONT color=#800000 size=2>"PrintSpread.aspx"</FONT><FONT size=2>);

    </FONT>

    New Print Page (PrintSpread.aspx):

    <FONT color=#008080 size=2>

    FpSpread</FONT><FONT size=2> mySpread = (</FONT><FONT color=#008080 size=2>FpSpread</FONT><FONT size=2>)Session[</FONT><FONT color=#800000 size=2>"MySpread"</FONT><FONT size=2>];


    PrintSpreadForm.Controls.Add(mySpread); <FONT color=#008000 size=2>//I get the error in this line

    </FONT></FONT>

    I must say that I'm adding a lot columns and rows at runtime, and they are fully customized using css styles. May it be an error on my particular spread control?


    Regards,


    Julio

  • Replied 8 September 2017, 9:28 am EST

    Julio,


    You can test that theory by creating a blank default Spread in the Session variable and seeing if that works. Can you create a small zipped project reproducing this issue to post for us to debug?

  • Replied 8 September 2017, 9:28 am EST

    Well, I have just done that and yes, it works. I made another test to try to simulate our scenario. I created a web user control and there I put an Infragistics WebTab, with 1 tab. In that tab, I put a FarPoint Spread control, with a SQLDataSource to bind to a SQL Server table. Then, I put my web user control in a aspx page and then I ran the page. And it works pretty well.


    As long as I can see, the Spread control works very well, it is a cool control. But in my particular scenario, print functionality just does not work, and I can't reproduce the error in a small project, due the complexities of my solution (lots controls, session variables, security features, frames, configuration files, and so on). 


    I have no more time to spend on this, so I just took an alternate aproach. Now I'm sending the spread data to Excel and from there the user can print with no problem. This also satisfies another requirement of my project (Excel exporting), so this solves my problem for now.


    I'll let you know if I get to need that printing functionality again in the future.


    Regards,


    Julio

  • Replied 8 September 2017, 9:28 am EST

    Hi Julio.


    <DIV id=result_box>I can help, I have the same problem you with the impression of the spread, he trying to fix some ways but can not find the solution.

    You find the solution? I could give a guide on how to solve this problem

    Thanks for the help.</DIV>

     


     

  • Replied 8 September 2017, 9:28 am EST

    Hi pvilca,


    I couldn't find the solution, but used a different approach exporting to Excel so the user can print from there. I actually don't care about this anymore as I don't even work for that company anymore.


    Thanks,


    Julio

  • Replied 17 October 2021, 4:26 pm EST

    There are <%...%> code blocks inside your Page.Header (which is referring to - possibly in a master page). This processing are made during the page render and not on code behind, on the events of the page. Therefore, when you try to add an item to the Controls collection of that control, you get this error message. So, start the code block with < %# instead of < %= : . This changes the [url=http://net-informations.com/faq/net/interview-questions.htm]C#[/url] code block from a Response.Write code block to a databinding expression. Since <%# ... %> databinding expressions aren't code blocks, the CLR won't complain.

  • Replied 7 November 2021, 4:49 pm EST

    Hi Julio,

    Is there any issue on which need help from our side? Please let me know if you have any issues.

    Regards,
    Avinash
Need extra support?

Upgrade your support plan and get personal unlimited phone support with our customer engagement team

Learn More

Forum Channels