views:

3212

answers:

5

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.

+1  A: 

This error is not specific to Ajax.

You could try putting your ajax:TabContainer inside an asp:Panel. Alternatively, remove the <% ...%> code blocks from your page.

Joe
There are no <% %> code blocks in my page! Furthermore, in the sample website, the TabContainer wasn't in an asp:Panel.
Cybis
+1  A: 

I've figured it out!

This is the error message you get if you attempt to use AJAX controls while your < head > contains a < script > tag.

I just moved the javascript into the body, and it seems to work fine now.

Cybis
WOW THANKS!!! I really, really hated this error. "The Controls collection cannot be modified because the control contains code blocks" What the hell is that supposed to mean!?
wallyqs
+4  A: 

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.

Keith
Accepted answer changed, considering you're the first to actually answer the question correctly. Thank you! However, there's no need for me to learn ASP.NET MVC now. I lost my job recently. Probably a good thing in the long run though, I can't stand web development anymore.
Cybis
A: 

Pragnesh, Check between the HEAD tags for any javascript. I had some javascript with the <%%> code-blocks that prevented the AJAX tab controls from working.

MAbraham1
A: 

Hey guys, it's again not just javascript tags, but anything within the head tags of your page that uses <%= %> or <% Response.Write %>. Either change your code to use the databinding directive <%# eval(some code) %> or move the stuff to the body (this is also useful for speeding up the loading time of your pages).

Mavusi