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" />: <asp:Label ID="lblSliderValue" runat="server" /> <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>
</table>
What is the problem?
I had the same issue with different circumstances.
I had simple element inside the body tag.
The solution was:
I also faced the same issue. I found the solutions like following.
Solution 1: I kept my script tag in the body.
Now conflicts regarding the tags will resolve.
Solution 2:
We can also solve this one of the above solutions like Replace the code block with <%# instead of <%= But the problem is it will give only relative path. If you want really absolute path it won't work.
Solution 1 works for me. Next is your choice.
The "<%#" databinding technique will not directly work inside <link> tags in the <head> tag:
The above code will evaluate to
Instead, you should do the following (note the two double quotes inside):
And you will get the desired result:
For some cases ResolveUrl and ResolveClientUrl works but not all times especially in case of js script files. What happens is it works for some pages but when you navigate to some other pages it might not work due to relative path of that particular page.
So finally my suggestion is always do a complete recheck of your site pages for whether all your javascript references are fine or not. Open your site in Google Chrome -> right click on the page -> click view source page -> HTML appears -> now click your JS hyperlinks; if its working fine it should open the js file in another browser window, otherwise it will not open.
In my case, I have replaced
<%= %>
with<%# %>
, and it worked!An alternative way is to have another .aspx page act as the page you want to link to.
This is what the header of the Masterpage looks like:
The referenced .aspx form contains your content:
Finally, you need the .aspx page to tell the browser you're sending CSS content: