Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > Problem with Events in UserControls dynamically added to AJAX UpdatePanel

Reply
Thread Tools

Problem with Events in UserControls dynamically added to AJAX UpdatePanel

 
 
JacekDr
Guest
Posts: n/a
 
      08-24-2007
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.Coun t.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"])
[i].GetType().BaseType == typeof(UserControls_Person))
{


UpdatePanel.ContentTemplateContainer.Controls.Add( ((ControlCollection)HttpContext.Current.Session["owners"])
[i]);
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

 
Reply With Quote
 
 
 
 
Muhammad Naveed Yaseen
Guest
Posts: n/a
 
      08-26-2007
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.

 
Reply With Quote
 
 
 
 
Muhammad Naveed Yaseen
Guest
Posts: n/a
 
      08-26-2007
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.

 
Reply With Quote
 
Muhammad Naveed Yaseen
Guest
Posts: n/a
 
      08-26-2007
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 <asplaceHolder 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.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Subscribing to custom events from dynamically created UserControls TJ ASP .Net 1 05-02-2008 04:11 AM
Maintaining the look of dynamically added usercontrols Nathan Sokalski ASP .Net Building Controls 7 06-19-2007 05:04 AM
Maintaining the look of dynamically added usercontrols Nathan Sokalski ASP .Net 7 06-19-2007 05:04 AM
CustomControls and Dynamically Added UserControls a ASP .Net Web Controls 1 11-12-2003 06:21 AM
Events not firing on dynamically added UserControls James Gerdes ASP .Net Building Controls 1 09-25-2003 03:41 AM



Advertisments