Click here to Skip to main content
15,886,689 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
I have created a web user control, using Ajax calenderExtender.

I want to load this control dynamically, on a page so that the user can create more controls as he wants by clicking on the link button.
The controls load first time, but on postback, the error occurs : "Extender controls may not be registered before PreRender"

Code Snippet For WebUserControl.ascx:

XML
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="WebUserControl.ascx.cs" Inherits="WebUserControl" %>
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="ajax" %>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<button runat="server" id="btn">SelectDate</button>
<ajax:CalendarExtender TargetControlID="TextBox1" PopupButtonID="btn" Animated="true" runat="server" PopupPosition="Right" ID="calenderExtender">
</ajax:CalendarExtender>
<ajax:ToolkitScriptManager runat="server" LoadScriptsBeforeUI="true" EnablePartialRendering="true" ID="scriptmanager"></ajax:ToolkitScriptManager></pre></code>
<b>Code Snippet For Default.aspx</b>:
<code><pre lang="xml"><%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>
<%@ Register Src="~/WebUserControl.ascx" TagPrefix="UC" TagName="Ucontrol" %>
<!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 runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <asp:PlaceHolder ID="placeholder" runat="server"></asp:PlaceHolder>
    <asp:Panel ID="panel" runat="server"></asp:Panel>
    </form>
</body>
</html>


Code Snippet For Default.aspx.cs

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Control c = this.LoadControl("WebUserControl.ascx");
            Panel pan1 = new Panel();
            pan1.Controls.Add(c);
            placeholder.Controls.Add(pan1);
            Session["controls"] = pan1;
        }
        else
        {
            Panel pan = Session["controls"] as Panel;
            placeholder.Controls.Add(pan);
        }
        LinkButton lb = new LinkButton();
        lb.Text = "Add more controls";
        lb.Click += new EventHandler(lb_Click);
        panel.Controls.Add(lb);
    }
    void lb_Click(object sender, EventArgs e)
    {
        Control c = this.LoadControl("WebUserControl.ascx");
        Panel pan = Session["controls"] as Panel;
        pan.Controls.Add(c);
        placeholder.Controls.Add(pan);
        Session["controls"] = pan;
    }
}

[edit]Code blocks sorted out - next time, don't copy and paste so much with "Auto detect" switched on: you translated < and > several times! - OriginalGriff[/edit]
Posted
Updated 10-Mar-11 21:23pm
v3
Comments
Dalek Dave 11-Mar-11 3:23am    
Edited for Readability.

1 solution

I think you may wish to try some of these links.
It is a very good idea to try a google search on the text of an error message because you usually get some good hits on solutions that way.
Cheers.
 
Share this answer
 
Comments
Espen Harlinn 15-Mar-11 4:33am    
Right, my 5

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900