views:

14551

answers:

7

I am trying to create a simple user control that is a slider. When I add a AjaxToolkit SliderExtender to the user control I get this (*&$#()@# error:

Server Error in '/' Application. The Controls collection cannot be modified because the control contains code blocks (i.e. <% ... %>). Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Web.HttpException: The Controls collection cannot be modified because the control contains code blocks (i.e. <% ... %>).

Source Error:

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

Stack Trace:

[HttpException (0x80004005): The Controls collection cannot be modified because the control contains code blocks (i.e. <% ... %>).]
System.Web.UI.ControlCollection.Add(Control child) +8677431
AjaxControlToolkit.ScriptObjectBuilder.RegisterCssReferences(Control control) in d:\E\AjaxTk-AjaxControlToolkit\Release\AjaxControlToolkit\ExtenderBase\ScriptObjectBuilder.cs:293 AjaxControlToolkit.ExtenderControlBase.OnLoad(EventArgs e) in d:\E\AjaxTk-AjaxControlToolkit\Release\AjaxControlToolkit\ExtenderBase\ExtenderControlBase.cs:306 System.Web.UI.Control.LoadRecursive() +50 System.Web.UI.Control.LoadRecursive() +141 System.Web.UI.Control.LoadRecursive() +141 System.Web.UI.Control.LoadRecursive() +141 System.Web.UI.Control.LoadRecursive() +141 System.Web.UI.Control.LoadRecursive() +141 System.Web.UI.Control.LoadRecursive() +141 System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +627

Version Information: Microsoft .NET Framework Version:2.0.50727.3074; ASP.NET Version:2.0.50727.3074

I have tried putting a placeholder in the user control and adding the textbox and slider extender to the placeholder programmatically and I still get the error.

Here is the simple code:

<table cellpadding="0" cellspacing="0" style="width:100%">
<tbody>
    <tr>
        <td></td>
        <td>
            <asp:Label ID="lblMaxValue" runat="server" Text="Maximum" CssClass="float_right" />
            <asp:Label ID="lblMinValue" runat="server" Text="Minimum" />
        </td>
    </tr>
    <tr>
        <td style="width:60%;">
            <asp:CheckBox ID="chkOn" runat="server" /><asp:Label ID="lblPrefix" runat="server" />:&nbsp;<asp:Label ID="lblSliderValue" runat="server" />&nbsp;<asp:Label ID="lblSuffix" runat="server" />
        </td>
        <td style="text-align:right;width:40%;">                

                <asp:TextBox ID="txtSlider" runat="server" Text="50" style="display:none;" />
                <ajaxToolkit:SliderExtender ID="seSlider" runat="server" 
                    BehaviorID="seSlider" 
                    TargetControlID="txtSlider" 
                    BoundControlID="lblSliderValue" 
                    Orientation="Horizontal" 
                    EnableHandleAnimation="true" 
                    Length="200" 
                    Minimum="0" 
                    Maximum="100" 
                    Steps="1" />

        </td>
    </tr>
</tbody>

What is the problem?

Thanks in advance.

Dan

+2  A: 

I had same issue in the user control. My page that was hosting the control had comments in the head tag, I removed those comments, everything worked afterwards. Some posts also suggest removing scripts from head and placing them in the body.

+16  A: 

First, start the code block with <%# instead of <%= :

<head id="head1" runat="server">
  <title>My Page</title>
  <link href="css/common.css" rel="stylesheet" type="text/css" />
  <script type="text/javascript" src="<%# ResolveUrl("~/javascript/leesUtils.js") %>"></script>
</head>

This changes the code block from a Response.Write code block to a databinding expression. Since <%# ... %> databinding expressions aren't code blocks, the CLR won't complain. Then in the code for the master page, you'd add the following:

protected void Page_Load(object sender, EventArgs e)
{
  Page.Header.DataBind();    
}

Check this great solution to the porblem

http://leedumond.com/blog/the-controls-collection-cannot-be-modified-because-the-control-contains-code-blocks/

jalchr
I don't get this error, but I'm using the = notation. However: I've seen my code fail with the above described problem. What can cause that (of why is it working OK with me)?
doekman
Are you using .net 4.0 ?
jalchr
Awesome. Much better solution than others I have had to use.
Joshua Hudson
A: 

I tried using <%# %> with no success. Then I changed Page.Header.DataBind(); in my code behind to this.Header.DataBind(); and it worked fine.

Nick
A: 

I tried using <%# %> with no success. Then I changed Page.Header.DataBind(); in my code behind to this.DataBind();, because I used the code in userControl and it dosen't have a header, and it worked fine.

mahser
A: 

I can confirm that moving the javascript with <% %> tags from the head to the form tag fixes this error

http://italez.wordpress.com/2010/06/22/ajaxcontroltoolkit-calendarextender-e-strana-eccezione/

ITalez
A: 

I'm using vb.net and solution with Me.DataBind() and <%# %> works well for me. Appreciate it.

Seba
A: 

you can do the same functionality if you are using script manager in your page. you have to just register the script like this

<asp:ScriptManager ID="ScriptManager1" runat="server" LoadScriptsBeforeUI="true" EnablePageMethods="true">
<Scripts> <asp:ScriptReference Path="~/Styles/javascript/jquery.min.js" /> </Scripts> </asp:ScriptManager>

Please reply me if you like the suggestion.

Mohan Prajapati