Problem with Events in UserControls dynamically added to AJAX UpdatePanel

  • Thread starter Thread starter JacekDr
  • Start date Start date
J

JacekDr

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
 
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.
 
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.
 
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.
 
Back
Top