Problem with Events in UserControls dynamically added to AJAX UpdatePanel

Discussion in 'ASP .Net' started by JacekDr, Aug 24, 2007.

  1. JacekDr

    JacekDr Guest

    Hello,

    I've got the following problem:

    I want to add and remove dynamically controls to UpdatePanel.
    In my user control I have a button, but when I click it I get
    AsyncPostback and Event for ButtonClick in UserControl doesn't fire.

    <asp:ScriptManager ID="ScriptManager" EnableViewState="true"
    runat="server" EnablePageMethods="True" >
    </asp:ScriptManager>
    <asp:UpdatePanel ID="UpdatePanel" EnableViewState="true"
    runat="server" ChildrenAsTriggers="true" UpdateMode="Conditional">
    <ContentTemplate>
    <asp:Button Text="+Add Person" ID="AddPerson"
    runat="server" OnClick="AddPerson_Click" />
    </ContentTemplate>
    </asp:UpdatePanel>
    <asp:Button Text="+Save" ID="SavePersons" runat="server"
    OnClick="SavePersons_Click" />

    Here is my code that adds new user control to UpdatePanel:

    protected void AddPerson_Click(object sender, EventArgs e)
    {
    UserControls_Person ctrl =
    (UserControls_Person)LoadControl("UserControls/Person.ascx");
    ctrl.ID = "Person" +
    UpdatePanel.ContentTemplateContainer.Controls.Count.ToString();
    UpdatePanel.ContentTemplateContainer.Controls.Add(ctrl);
    HttpContext.Current.Session.Remove("owners");
    HttpContext.Current.Session.Add("owners",
    UpdatePanel.ContentTemplateContainer.Controls);
    UpdatePanel.Update();
    }

    After postback I create controls in CreateChildControls:
    protected override void CreateChildControls()
    {
    base.CreateChildControls();
    if (Page.IsPostBack)
    {
    if (HttpContext.Current.Session["owners"] != null)
    {
    for (int i = 0; i <
    ((ControlCollection)HttpContext.Current.Session["owners"]).Count; i++)
    {

    if
    (((ControlCollection)HttpContext.Current.Session["owners"])
    .GetType().BaseType == typeof(UserControls_Person))
    {


    UpdatePanel.ContentTemplateContainer.Controls.Add(((ControlCollection)HttpContext.Current.Session["owners"])
    );
    i--;
    }
    }
    }

    HttpContext.Current.Session.Remove("owners");
    HttpContext.Current.Session.Add("owners",
    UpdatePanel.ContentTemplateContainer.Controls);
    }
    }

    In my UserControl in Page_Load method I have:

    this.DelOwner.Click += new EventHandler(DelOwner_Click);

    Could U help me what did I wrong?

    BR,
    Jack
    JacekDr, Aug 24, 2007
    #1
    1. Advertising

  2. Life is quite beautiful without dynamic addition-removal of controls.
    Avoid making it ugly if same thing can be done by style="display:none/
    block" or Visible="true/false'.

    If the employer is hell bent to keep it ugly for you, then first test
    whether the event fires at all without ajax update panel, if it
    doesn't then you are not subscribing to same event reference which was
    sent in previous render. If it works fine without ajax update panel,
    then to make it work in ajax panel call RegisterAsyncPostBackControl
    on ScriptManager at each dynamic addition.
    Muhammad Naveed Yaseen, Aug 26, 2007
    #2
    1. Advertising

  3. Please also know that if we dynamically change post behavior of
    controls via RegisterPostBackControl or RegisterAsyncPostBackControl,
    it is our responsibility to call UpdatePanel.Update() in each post
    back originating from these controls.
    Muhammad Naveed Yaseen, Aug 26, 2007
    #3
  4. Life is quite beautiful without dynamic addition-removal of controls.
    Avoid making it ugly if same thing can be done by <div
    style="display:none/block"> or <asp:placeHolder Visible="true/
    false'...


    If the employer is hell bent to keep it ugly for you, then first test
    whether the event fires at all without ajax update panel, if it
    doesn't then you are not subscribing to same event reference which was
    sent in previous render. If it works fine without ajax update panel,
    then to make it work in ajax panel call RegisterAsyncPostBackControl
    on ScriptManager at each dynamic addition.

    Please also know that if we dynamically change post behavior of
    controls via RegisterPostBackControl or RegisterAsyncPostBackControl,
    it is our responsibility to call UpdatePanel.Update() in each post
    back originating from these controls.
    Muhammad Naveed Yaseen, Aug 26, 2007
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Nathan Sokalski
    Replies:
    7
    Views:
    359
    Cor Ligthert [MVP]
    Jun 19, 2007
  2. Arachnid
    Replies:
    0
    Views:
    773
    Arachnid
    Oct 5, 2007
  3. James Gerdes

    Events not firing on dynamically added UserControls

    James Gerdes, Sep 23, 2003, in forum: ASP .Net Building Controls
    Replies:
    1
    Views:
    271
    Victor Garcia Aprea [MVP]
    Sep 25, 2003
  4. Nathan Sokalski

    Maintaining the look of dynamically added usercontrols

    Nathan Sokalski, Jun 16, 2007, in forum: ASP .Net Building Controls
    Replies:
    7
    Views:
    747
    Cor Ligthert [MVP]
    Jun 19, 2007
  5. a
    Replies:
    1
    Views:
    192
    Teemu Keiski
    Nov 12, 2003
Loading...

Share This Page