Addhandler, button.click not firing using VB.NET

user649904 picture user649904 · Mar 8, 2011 · Viewed 15.5k times · Source

I am experiencing a problem with buttons and AddHandler. It only works if I use AddHandler Button1.click, AddressOf... in Page_load, but if I create the button dynamically in one of the sub routines, the event doesn't fire.

For example,

<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True">
    <asp:ListItem>1</asp:ListItem>
    <asp:ListItem>2</asp:ListItem>
</asp:DropDownList>
<asp:ScriptManager id="ScriptManager1" runat="server"></asp:ScriptManager>
<asp:UpdatePanel id="UpdatePanel1" runat="server" UpdateMode="Conditional" ChildrenAsTriggers="False">
    <contenttemplate>
        <asp:PlaceHolder id="PlaceHolder1" runat="server"></asp:PlaceHolder>
     </contenttemplate>
</asp:UpdatePanel>
<asp:UpdatePanel id="UpdatePanel2" runat="server" UpdateMode="Conditional">
    <contenttemplate>
        <asp:Label id="Label2" runat="server" Text="Label"></asp:Label>
    </contenttemplate>
</asp:UpdatePanel>


Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
    Label1.Text = Date.Now
    ScriptManager1.RegisterAsyncPostBackControl(DropDownList1)
End Sub

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs)
    Label2.Text = "Panel refreshed at " + Date.Now.ToString()
End Sub

Protected Sub DropDownList1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles DropDownList1.SelectedIndexChanged
    Dim b As New Button
    b.Text = "Click"
    ScriptManager1.RegisterAsyncPostBackControl(b)
    AddHandler b.Click, AddressOf Button1_Click
    PlaceHolder1.Controls.Add(b)
    UpdatePanel1.Update()
End Sub

The dropdownlist works, but the button doesn't. What am I doing wrong?

Answer

Tim Schmelter picture Tim Schmelter · Mar 8, 2011

You have to regenerate your dynamically created controls on every postback (at last in Page_Load, better in Page_Init). You have to set the ID of the controls accordingly because ASP.Net needs it to identify which control caused a Postback and to handle the appropriate events.

You could save the number of created buttons in ViewState and use this to regenerate them on Page_Load. Increase the number when you add a new button. Use this number also to make the Button's ID unique(append it to the ID) to ensure that its the same on every postback.

For further informations, have a look the Page-Lifecycle and ViewState with dynamically added controls.

Edit: As Joel commented, if you only need one Button you can set it's ID statically, but you have to regenerate it on postback f.e. to handle its click-event.