Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / ASP.NET / General / August 2007

Tip: Looking for answers? Try searching our database.

Problem with Events in UserControls dynamically added to AJAX UpdatePanel

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
JacekDr - 24 Aug 2007 15:27 GMT
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"])
[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
Muhammad Naveed Yaseen - 26 Aug 2007 01:17 GMT
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 - 26 Aug 2007 01:33 GMT
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 - 26 Aug 2007 01:53 GMT
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.

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.