Dynamic buttons loose click event on postback

2 posts, 0 answers
  1. Eric S.
    Eric S. avatar
    3 posts
    Member since:
    Oct 2009

    Posted 13 Jan 2010 Link to this post

    Hi all,

    In Psuedo code this what I'm trying to achieve:

    User comes to page with their Customer profile
    if user clicks "Address Information"
       populate pnlContent with Addresses_usercontrol - dynamic buttons are created in this user control
    end if
    If user clicks "Change this address" - dynamic button with AddressID
      clear pnlContent()
      populate pnlContent with EditAddress_usercontrol
    end if




    First off, on my main page I have a RadSplitter with two panes

     <telerik:RadSplitter ID="RadSplitter1" runat="server" Height="100%" Width="100%"
         <telerik:RadPane ID="LeftPane" runat="server" Width="180px" Height="100%"
             <asp:Panel runat="server" ID="PnlButtons"
                 <asp:Button runat="server" ID="CusInfo" Text="Customer Information" /> 
                 <br /> 
                 <asp:Button runat="server" ID="CusCC" Text="Credit Card Information" /> 
                 <br /> 
                 <asp:Button runat="server" ID="CusAddr" Text="Address Information" /> 
                 <br /> 
                 <asp:Button runat="server" ID="CusContact" Text="Contact Information" /> 
                 <br /> 
                 <asp:Button runat="server" ID="CusContPrefs" Text="Contact Preferences" /> 
                 <br /> 
                 <asp:Button runat="server" ID="CusDemo" Text="Demographic Information" /> 
                 <br /> 
                 <asp:Button runat="server" ID="CusOrders" Text="Order History" /> 
                 <br /> 
             </asp:Panel> 
         </telerik:RadPane> 
         <telerik:RadSplitBar ID="RadSplitBar1" runat="server" CollapseMode="None" /> 
         <telerik:RadPane ID="MiddlePane" runat="server" Scrolling="None" Width="400px" Height="100%"
            <telerik:RadAjaxPanel ID="RadAjaxPanel1" runat="server" Width="400px" LoadingPanelID="LoadingPanel1"
                <asp:Panel ID="pnlContent" runat="server"
                </asp:Panel> 
            </telerik:RadAjaxPanel> 
         </telerik:RadPane> 
    </telerik:RadSplitter>  

    I am using the buttons in the pnlButtons to populate a userControl in the pnlContent. This all works fine. However, when a userControl is loaded into pnlContent there are dynamic buttons that are created with addeventhandlers (VB) .  Essentially these buttons will clear the userControl in pnlContent and populate with another userControl.

    Dim newbtn As New Button() 
    newbtn.Text = "Add New!" 
    newbtn.ID = "btn" 
     
    AddHandler newbtn.Click, AddressOf DynamicClick 

    Since I have dynamic buttons being created their click events are not being fired, I figure because postback has already happened. How can i persist the click event for these dynamic buttons in VB?
  2. Tsvetie
    Admin
    Tsvetie avatar
    1517 posts

    Posted 19 Jan 2010 Link to this post

    Hello Eric S.,
    When you add controls dynamically to a page, you have to make sure that you add those controls in the same place on the page, when the page postbacks or one of their parents get updated during an AJAX request. In your case, when you click the button, the part of the page that is wrapped in the AjaxPanel gets updated and any dynamically added controls in it will be lost in case you do not recreate them.

    You can find more information about dynamically adding controls with AJAX on the NET. Below are a few links that can help you:

    All the best,
    Tsvetie
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
Back to Top