Search code examples
visual-studio-2008ajaxcontroltoolkit

Trying to add ajax TabContainer, getting error "The Controls collection cannot be modified because the control contains code blocks (i.e. <% ... %>)."


I want to add an ajax:TabContainer to my webpage. I don't get any build errors, but when I try to browse to the page, it gives me the error: "The Controls collection cannot be modified because the control contains code blocks (i.e. <% ... %>).".

I re-downloaded the Ajax Control Toolkit for the sample sites, opened the solution in VS, ran the sample for the TabContainer, and it worked fine. I thought it was perhaps a different version of the Ajax Control Toolkit - but no. The AjaxControlToolkit.dll files being referenced by the two sites are identical. Why can't I get the TabContainer to work on my site?

There is one more issue, but I don't know whether it's related. I just recently installed Visual Studio 2008. As soon as I opened my website, VS automatically created the tab "AJAX Controls" in the toolbox and filled it with all the ajax controls. In the source code, all controls are prefixed with "ajax" - i.e., "< ajax:TabContainer runat="server" ... >".

However, when I opened the sample website, Visual studio created another tab in the toolbox - "AjaxControlToolkit Components", filled with all the same controls as in "AJAX Controls". I don't know why it added the same controls twice (but, strangely enough, with different icons for them in the toolbox). In the source code, all controls are prefixed with "ajaxToolkit" - i.e., "< ajaxToolkit:TabContainer runat="server" ... >". What's going on here? I just want the darn TabContainer to work on my site.


Solution

  • You can't use <%= %> (write) blocks inside a control that uses the standard server rendering - you get this error.

    In order for the ASP AJAX components to work you need:

    <head runat="server">...
    

    Otherwise it crashes with this error too.

    However you can databind inside these server controls:

    <head runat="server">
        <link rel="stylesheet" type="text/css" 
            href="<%# ResolveUrl( "~/styles/common.aspx" ) %>" />
    ...
    

    And then in your page load:

    Page.Header.DataBind();
    

    The error occurs due to the way ASP WebForms render controls as component collections - they can handle either the collection (and expect databind <%#) or literal writes (and expect <%=) but not both together.

    The best way to avoid this issue permanently is to switch to ASP MVC.