We help IT Professionals succeed at work.

CalendarExtender - Exception is generated when using this control...

We are using Visual Studio Professional 2005, and the Ajax Control Toolkit version 1.0.10618.0

When running the attached code snippet in an aspx page I get the following stack trace:

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) +2108047
   AjaxControlToolkit.ScriptObjectBuilder.RegisterCssReferences(Control control) +757
   AjaxControlToolkit.ExtenderControlBase.OnPreRender(EventArgs e) +115
   System.Web.UI.Control.PreRenderRecursiveInternal() +86
   System.Web.UI.Control.PreRenderRecursiveInternal() +170
   System.Web.UI.Control.PreRenderRecursiveInternal() +170
   System.Web.UI.Control.PreRenderRecursiveInternal() +170
   System.Web.UI.Control.PreRenderRecursiveInternal() +170
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +2041

Thank you in advance for any suggestions.


<asp:TextBox ID="txtTest" runat="server"></asp:TextBox>
<cc1:CalendarExtender ID="CalendarExtender1" runat="server" TargetControlID="txtTest" Format="MM/dd/yyyy" CssClass="MyCalendar">
</cc1:CalendarExtender>

Open in new window

Comment
Watch Question

Commented:
I believe you need to have a button to trigger the calendar extender:

<asp:TextBox ID="txtStart" CssClass="TextBox" runat="server" Width="155px" />&nbsp;
<asp:ImageButton runat="Server" ID="btnCalS" ImageUrl="graphics/Icons/Calendar3.png" AlternateText="Click to show start date calendar" />&nbsp;                          <cc1:CalendarExtender ID="cldStart" TargetControlID="txtStart" PopupButtonID="btnCalS" Format="MM/dd/yyyy" runat="server" />

Author

Commented:
Thanks for the response.

Well according to asp.net you can implement the extender with or without the . I have tried what you suggested but i am still getting the same runtime error.

Commented:
Is your MyCalendar css class setup?
Commented:
Please check: http://forums.asp.net/p/1172295/2108775.aspx#2108775

I beleive there is a solution there, it might be something to do with your Page Directive at the top of your page.

Author

Commented:
Thank you - that's exactly what it was.  I wrapped the following code:

<script type="text/javascript" src="<%=ResolveClientUrl"~/JavaScripts/JSFunctions_UI.js") %>

...in the MasterPage with