Saturday, March 29, 2014

How to fix the error: “Extender controls may not be registered after prerender” when add multi user controls

I have a Admin page. In this page, I have a div, that will contain 2 user controls in it. The Manager.aspx file like that:



<%@ Page Title="" Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="Manager.aspx.cs" Inherits="Admin.Manager" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
<title>Admin Manager</title>
</asp:Content>
<asp:Content ID="Content5" ContentPlaceHolderID="contentRight" Runat="Server">
<div class="grid_16" runat="server" id="divUserControl"></div>
</asp:Content>

In code behind of Manager.aspx.cs, I will add dynamically user controls like that:



protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
var userControls = new List<Control> {};
var userManager = TemplateControl.LoadControl("~/MyControls/UserManager.ascx");
userControls.Add(userManager);
var roleManager= TemplateControl.LoadControl("~/MyControls/RoleManager.ascx");
userControls.Add(roleManager);
Sessions.UserControl = userControls;
}
SelectControl();
}

private void SelectControl()
{
var userControls = Sessions.UserControl;
divUserControl.Controls.Clear();
if (!string.IsNullOrEmpty(Request.QueryString["uc"]))
{
var query = int.Parse(Request.QueryString["uc"]);
switch (query)
{
case 1:

divUserControl.Controls.Add(userControls[0]);
break;
case 2:
divUserControl.Controls.Add(userControls[1]);
break;
default:
divUserControl.Controls.Add(userControls[0]);
break;
}
}
else
{
divUserControl.Controls.Add(userControls[1]);
}
}

I had searched google to find solution, and insert the code below in the Manager.aspx.cs:



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

Or the solution:



protected override void OnInit(EventArgs e)
{
base.OnInit(e);
if (this.DesignMode == true)
{
this.EnsureChildControls();
}
this.Page.RegisterRequiresControlState(this);
}

in the user control UserManager.ascx.cs.


But It's still raise error.



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) +349151
System.Web.UI.ScriptManager.RegisterExtenderControl(TExtenderControl extenderControl, Control targetControl) +69
System.Web.UI.ExtenderControl.RegisterWithScriptManager() +143
System.Web.UI.ExtenderControl.OnPreRender(EventArgs e) +19
AjaxControlToolkit.ExtenderControlBase.OnPreRender(EventArgs e) in f:\TeamCity\buildAgent\work\80acd78aa4c25314\Server\AjaxControlToolkit\ExtenderBase\ExtenderControlBase.cs:365
System.Web.UI.Control.PreRenderRecursiveInternal() +80
System.Web.UI.Control.PreRenderRecursiveInternal() +171
System.Web.UI.Control.PreRenderRecursiveInternal() +171
System.Web.UI.Control.PreRenderRecursiveInternal() +171
System.Web.UI.Control.PreRenderRecursiveInternal() +171
System.Web.UI.Control.PreRenderRecursiveInternal() +171
System.Web.UI.Control.PreRenderRecursiveInternal() +171
System.Web.UI.Control.PreRenderRecursiveInternal() +171
System.Web.UI.Control.PreRenderRecursiveInternal() +171
System.Web.UI.Control.PreRenderRecursiveInternal() +171
System.Web.UI.Control.PreRenderRecursiveInternal() +171
System.Web.UI.Control.PreRenderRecursiveInternal() +171
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +842


No comments:

Post a Comment