Get Help:Ask a Question in our Forums|Report a Bug|More Help Resources
Last post May 01, 2012 02:14 PM by Ruchira
May 01, 2012 05:11 AM|LINK
I have added dynamically created buttons but I bumped into a problem. I can't seem to get Count and Remove buttons working (Add button works fine). They just removes themselves (it's fine with Remove button but Count should add +1 to label value).
What should I do to get them working properly?
Here's my code:
Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs)
Dim btn As New Button()
btn.ID = "btn"
btn.Text = "Add"
AddHandler btn.Click, AddressOf btn_Click
Sub btn_Click(ByVal sender As Object, ByVal e As System.EventArgs)
Dim btn1 As New Button()
btn1.ID = "btn1"
btn1.Text = "Count"
AddHandler btn1.Click, AddressOf btn1_Click
Dim btn2 As New Button()
btn2.ID = "btn2"
btn2.Text = "Remove"
AddHandler btn2.Click, AddressOf btn2_Click
Label1.Text = "0"
Sub btn1_Click(ByVal sender As Object, ByVal e As EventArgs)
Label1.Text += 1
Sub btn2_Click(ByVal sender As Object, ByVal e As EventArgs)
<head id="Head1" runat="server">
<form id="form1" runat="server">
<asp:PlaceHolder ID="PlaceHolder1" runat="server" /><br />
<asp:PlaceHolder ID="PlaceHolder2" runat="server" /><br />
<asp:Label runat="server" ID="Label1" />
May 01, 2012 06:07 AM|LINK
I would suggest you to maintain a hidden variable.
May 01, 2012 02:14 PM|LINK
I can't seem to get Count and Remove buttons working (Add button works fine)
Add button works fine because you are creating it in the Page_Init() method. But you are creating the Count and Remove buttons on the click event of the Add button. Dynamically generated controls will not be persistent across posts back. You have to create
them every time with post backs or you have to keep them in session and load them from the session. But either way, you have to do it in every post back. Otherwise they will not be retained on the page once a post back occurs.
Please 'Mark as Answer' if this post helps you.