ASP.NET error:  The Controls collection cannot be modified ...

Geoff Sutton
Geoff Sutton used Ask the Experts™
on
I am working on a modular project which runs off of the single main page default.aspx.  The page contains an iFrame which has the source changing depending on button clicks.  It seems to be working fine, and I can dynamically modify the order that the pages appear in when clicking "Next".  However, with one page I keep getting this strange error.  Typically it appears with a master page, or at least when trying to modify the control collection etc.  But this page has no master page, it is not modifying the controls collection, and it has no inline codeblocks.  The error occurs at the end of the onLoad event, but cannot determine why.  The full error text is below and I am including the page source as well.

Thanks,

Geoff



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) +8678663
   AjaxControlToolkit.ScriptObjectBuilder.RegisterCssReferences(Control control) +619
   AjaxControlToolkit.ExtenderControlBase.OnLoad(EventArgs e) +53
   System.Web.UI.Control.LoadRecursive() +50
   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.4200; ASP.NET Version:2.0.50727.4016
ASPX.CS code file - Only including the Page_Load function as this is only thing called.

using System;
using System.Linq;
using System.Web.UI;

public partial class Certificates_Medical : System.Web.UI.Page
{
    Harbourview.FuneralsDataContext dc = new Harbourview.FuneralsDataContext();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            if (Request.QueryString["fID"] == null)
            {
                Response.Redirect("../default.aspx");
            }

                string id = (Request.QueryString["fID"]);
            var dat = (from row in dc.tblDeceasedInfos
                       where row.funeralID == id
                       select row).FirstOrDefault();

            if (dat != null)
            {
                if(dat.med_DeathDate.HasValue){
                    txtDeathDate.Text=string.Format("{0:MM/dd/yyyy}", dat.med_DeathDate.Value);
                }

                if(!string.IsNullOrEmpty(dat.med_deathlocation))
                    txtDeathlocation.Text=dat.med_deathlocation;
                if(!string.IsNullOrEmpty(dat.med_doctorName))
                    txtDocName.Text=dat.med_doctorName;
                if(!string.IsNullOrEmpty(dat.med_deathHospital))
                    txtHospital.Text=dat.med_deathHospital;
                
            }
        }
    }


.ASPX HTML code - Complete page

<%@ Page Title="" Language="C#" AutoEventWireup="true" CodeFile="~/certificates/modules/Medical.aspx.cs"
    Inherits="Certificates_Medical" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title></title>

    <script type="text/javascript">
        function callParent() {
            javascript: parent.moveNext();
        }

        function saveData() {
            var btn = document.getElementById('<%=btnSave.ClientID %>');
            btn.click();
        }
    
    </script>

</head>
<body>
    <form id="form1" runat="server">
    <table>
        <tr>
            <td>
                Dr's Name
            </td>
            <td>
                <asp:TextBox ID="txtDocName" runat="server"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td>
                Hospital
            </td>
            <td>
                <asp:TextBox ID="txtHospital" runat="server"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td>
                Location
            </td>
            <td>
                <asp:TextBox ID="txtDeathlocation" runat="server"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td>
                Date of Death
            </td>
            <td>
                <asp:TextBox ID="txtDeathDate" runat="server"></asp:TextBox>
                <ajx:CalendarExtender ID="txtDOD_CAL" runat="server" TargetControlID="txtDeathDate">
                </ajx:CalendarExtender>
            </td>
        </tr>
    </table>
    <asp:Button ID="btnSave" runat="server" Text="" Style="visibility: hidden;" OnClick="btnSave_Click" />
    </form>
</body>
</html>

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Commented:
The <head> tag should not have -runat="server"

Change <head id="Head" runat="server">

to

<head id="Head">

-not related but related but, CalendareExtender need register tag.
Geoff SuttonSr. Software Developer

Author

Commented:
I will give this a try this evening thanks.  The Calendar Extender and rest of the AJAX control toolkit are registered in the web.config.  I use extenders such as this so frequently that it's much simpler to have it available all the time.
Geoff
Geoff SuttonSr. Software Developer

Author

Commented:
As I think about this, I have run into this issue before with the Calendar Extender.  Looking at the stack trace i notice that the problem occurs in the AJAX control toolkit code, which says that it must be the Calendar Extender.  So what can be done to use the extender without issues?
Geoff
Introduction to Web Design

Develop a strong foundation and understanding of web design by learning HTML, CSS, and additional tools to help you develop your own website.

Commented:
Long shot, but get the latest version of the Toolkit
Geoff SuttonSr. Software Developer

Author

Commented:
I think I already have it, but will download the newest tonight.  Good idea.  Thanks.
Geoff SuttonSr. Software Developer

Author

Commented:
I've followed both suggestions, no success.  There seems to be a conflict between the Calendar Extender and a page in an iFrame.  It works perfectly without the extender, and also with the extender as a stand alone page.
Still looking for solutions.
Geoff
Sr. Software Developer
Commented:
Ended up using a workaround - Using the asp:calendar control rather than the popup calendar.  It's not as fancy looking, but it does the job.
Geoff

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial