This is a migrated thread and some comments may be shown as answers.

Dynamic buttons loose click event on postback

1 Answer 73 Views
Ajax
This is a migrated thread and some comments may be shown as answers.
Eric S.
Top achievements
Rank 1
Eric S. asked on 13 Jan 2010, 08:24 PM
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?

1 Answer, 1 is accepted

Sort by
0
Tsvetie
Telerik team
answered on 19 Jan 2010, 09:57 AM
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.
Tags
Ajax
Asked by
Eric S.
Top achievements
Rank 1
Answers by
Tsvetie
Telerik team
Share this question
or