Extender Control may not be registered after PreRender when using AjaxCalenderExtender
<p>I am getting the error Extender Control may not be registered after PreRender. After searching the web I got answers like adding Base.PreRender(e) in OnPreRender method, and in OnInit method() call this.EnsureChildControls. But I applied and tried the answers but in vain. I am usign Master Page. Let me paste my code here..</p>

<p>My ASPX page is as follows:</p>

<pre><code>&lt;%@ Page  Language="C#" MasterPageFile="~/Test.master" AutoEventWireup="true"
    CodeFile="Report.aspx.cs" EnableEventValidation="false" Inherits="Report" Title="Reports" %&gt;
&lt;%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="cc1" %&gt;

&lt;asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server"&gt;
&lt;center&gt;
        &lt;asp:UpdatePanel ID="UpdatePanel" runat="server" UpdateMode="Conditional"&gt;            
            &lt;ContentTemplate&gt;
 &lt;div style="width: 100%;" runat="server" id="divaccount1"&gt;
                                            &lt;table width="100%"&gt;
                                                &lt;tr&gt;
                                                    &lt;td colspan="2" width="100%" id="lblMsg" runat="server" align="Center" style="font: bold;
                                                        color: Red; font-size: 12px; font-weight: bold;"&gt;
                                                    &lt;/td&gt;
                                                &lt;/tr&gt;
                                                &lt;tr&gt;
                                                &lt;td colspan="2"&gt;
                                                &lt;asp:Panel ID="pnlControls" runat="server"&gt;&lt;/asp:Panel&gt;
                                                &lt;/td&gt;
                                                &lt;/tr&gt;
                                                &lt;tr&gt;
                                                    &lt;td align="right"&gt;
                                                        &lt;asp:Button ID="btnReset" runat="server"  Text="Reset" OnClick="btnReset_Click" /&gt;
                                                    &lt;/td&gt;
                                                    &lt;td align="center"&gt;
                                                        &lt;asp:Button ID="btnGenerateReport" runat="server"  Text="Submit"
                                                            OnClick="btnGenerateReport_Click" /&gt;
                                                    &lt;/td&gt;
                                                &lt;/tr&gt;
                                            &lt;/table&gt;
                                            &lt;/div&gt;
 &lt;/ContentTemplate&gt;
        &lt;/asp:UpdatePanel&gt;
    &lt;/center&gt;
&lt;/asp:Content&gt;
</code></pre>

<p>My CodeFile contains </p>

<pre><code>public partial class Reports: System.Web.UI.Page
{
    static Table tblDynamicTable = new Table();
    protected override void OnPreRender(EventArgs e)
    {

        base.OnPreRender(e);       

    }
    protected override void OnInit(EventArgs e)
    {
       base.OnInit(e);

        if (this.DesignMode == true)
        {
            this.EnsureChildControls();
        }
        this.Page.RegisterRequiresControlState(this);
    }
     protected void Page_Load(object sender, EventArgs e)
    {
        try
        {


            tblDynamicTable.ID = "DynamicReportTbl";
            pnlControls.Controls.Add(tblDynamicTable);
         if (!IsPostBack)
            {
         try
                {
            tblDynamicTable.Rows.Clear();
                    tblDynamicTable.Dispose();
                    GetDynamciControls();
        }
        catch(Exception exx)
        {}
       }
      }
      public void GetDynamciControls()
      {

      int iNewRow = 0;
        TableRow rowTR = null;


    // Getting the value from DB and saving it in DataTable - dtRptFields

         foreach (DataRow dtRow in dtRptFields.Rows) 
        {
            if ((iNewRow == 0) &amp;&amp; (iNewRow % 4 == 0))
            {
                rowTR = new TableRow();
                rowTR.ID = "row" + iNewRow;
            }


            TableCell cellTDLbl = new TableCell();
            Label lb = new Label();
            lb.ID = "lbl" + dtRow["colname"].ToString().Trim();
            lb.Text = string.Format("{0}  : ", sDisplayName);
            cellTDLbl.Controls.Add(lb);
            rowTR.Cells.Add(cellTDLbl);
            iNewRow++;

            switch (dtRow["FieldType"].ToString().Trim().ToUpper())
            {
                case "TEXT":
                    TextBox tb = new TextBox();
                    TableCell cellTDTxt = new TableCell();
                    tb.ID = "txt" + dtRow["colname"].ToString() + dtRow["colname"].ToString().Trim();               
                    cellTDTxt.Controls.Add(tb);
                    rowTR.Cells.Add(cellTDTxt);


                    if (dtRow["Showcal"].ToString().Trim().ToUpper() == "TRUE")
                    {
                        TableCell cellTDCal = new TableCell();

                        System.Web.UI.WebControls.Image imgBtn = new System.Web.UI.WebControls.Image();
                        imgBtn.ID = "ImgDate" + iNewRow;
                        imgBtn.ImageUrl = "~/images/Calendar.png";
                        cellTDCal.Controls.Add(imgBtn);
                        rowTR.Cells.Add(cellTDCal);


                        AjaxControlToolkit.CalendarExtender calenderDate = new AjaxControlToolkit.CalendarExtender();
                        calenderDate.ID = "calenderDate" + iNewRow;
                        calenderDate.PopupButtonID = imgBtn.ID;
                        calenderDate.TargetControlID = tb.ID;
                        calenderDate.CssClass = "cal_Theme1";
                        calenderDate.Format = "MM/dd/yyyy";
                        cellTDCal.Controls.Add(calenderDate);
                        rowTR.Cells.Add(cellTDCal);
                    }

                    iNewRow++;
                    break;

                default:
                    break;
            }
            tblDynamicTable.Rows.Add(rowTR);          
        }
        pnlControls.Controls.Add(tblDynamicTable);
  }
  protected void btnGenerateReport_Click(object sender, EventArgs e)
    {
        try
        {
          ContentPlaceHolder mpContentPlaceHolder;
            TextBox mpTextBox;
            mpContentPlaceHolder =
              (ContentPlaceHolder)Master.FindControl("ContentPlaceHolder1");
            if (mpContentPlaceHolder != null)
            {
           Panel ctrlPanel =
                    (Panel)mpContentPlaceHolder.FindControl("pnlControls");
                if (ctrlPanel != null)
                {
                    foreach (Control childControl in ctrlPanel.Controls)
                    {
                        mpTextBox =
                  (TextBox)mpContentPlaceHolder.FindControl("txt1FromDt");
                        if (mpTextBox != null)
                        {
                           //My Core logic comes here
                        }
                }
                }
            }
    }
        catch (Exception ex)
        {
          }
    }
</code></pre>

<p>Since I am creating AjaxCalendarExtender dyanamically, I am getting the below error. I googled and implemented PreRender, OnInit methods also. But still I am getting this error. I am usign VS2010 - Framework 4.0. I am using MasterPage. In Masterpage also I tried to put preRender, onInit methods but vain. </p>

<p>Kindly help me to resolve this issue..</p>

<p>I am getting the following Error</p>

<pre><code>Extender controls may not be registered after PreRender.
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.InvalidOperationException: Extender controls may not be registered after PreRender.

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: 



[InvalidOperationException: Extender controls may not be registered after PreRender.]
   System.Web.UI.ScriptControlManager.RegisterExtenderControl(TExtenderControl extenderControl, Control targetControl) +432707
   System.Web.UI.ScriptManager.RegisterExtenderControl(TExtenderControl extenderControl, Control targetControl) +95
   System.Web.UI.ExtenderControl.RegisterWithScriptManager() +176
   System.Web.UI.ExtenderControl.OnPreRender(EventArgs e) +19
   AjaxControlToolkit.ExtenderControlBase.OnPreRender(EventArgs e) in d:\hg\act\Server\AjaxControlToolkit\ExtenderBase\ExtenderControlBase.cs:365
   System.Web.UI.Control.PreRenderRecursiveInternal() +103
   System.Web.UI.Control.PreRenderRecursiveInternal() +175
   System.Web.UI.Control.PreRenderRecursiveInternal() +175
   System.Web.UI.Control.PreRenderRecursiveInternal() +175
   System.Web.UI.Control.PreRenderRecursiveInternal() +175
   System.Web.UI.Control.PreRenderRecursiveInternal() +175
   System.Web.UI.Control.PreRenderRecursiveInternal() +175
   System.Web.UI.Control.PreRenderRecursiveInternal() +175
   System.Web.UI.Control.PreRenderRecursiveInternal() +175
   System.Web.UI.Control.PreRenderRecursiveInternal() +175
   System.Web.UI.Control.PreRenderRecursiveInternal() +175
   System.Web.UI.Control.PreRenderRecursiveInternal() +175
   System.Web.UI.Control.PreRenderRecursiveInternal() +175
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +2496
</code></pre>

<p>Regards,</p>

<p>T.N.Nagasundar</p>
