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:
<script runat="server">
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
PlaceHolder1.Controls.Add(btn)
End Sub
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
PlaceHolder2.Controls.Add(btn1)
Dim btn2 As New Button()
btn2.ID = "btn2"
btn2.Text = "Remove"
AddHandler btn2.Click, AddressOf btn2_Click
PlaceHolder2.Controls.Add(btn2)
Label1.Text = "0"
End Sub
Sub btn1_Click(ByVal sender As Object, ByVal e As EventArgs)
Label1.Text += 1
End Sub
Sub btn2_Click(ByVal sender As Object, ByVal e As EventArgs)
End Sub
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>ASP.NET TEST</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:PlaceHolder ID="PlaceHolder1" runat="server" /><br />
<asp:PlaceHolder ID="PlaceHolder2" runat="server" /><br />
<asp:Label runat="server" ID="Label1" />
</div>
</form>
</body>
</html>
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.
dzhyloff
0 Points
1 Post
ASP.NET dynamic buttons
May 01, 2012 05:11 AM|LINK
Hello,
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:
<script runat="server"> 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 PlaceHolder1.Controls.Add(btn) End Sub 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 PlaceHolder2.Controls.Add(btn1) Dim btn2 As New Button() btn2.ID = "btn2" btn2.Text = "Remove" AddHandler btn2.Click, AddressOf btn2_Click PlaceHolder2.Controls.Add(btn2) Label1.Text = "0" End Sub Sub btn1_Click(ByVal sender As Object, ByVal e As EventArgs) Label1.Text += 1 End Sub Sub btn2_Click(ByVal sender As Object, ByVal e As EventArgs) End Sub </script> <html xmlns="http://www.w3.org/1999/xhtml"> <head id="Head1" runat="server"> <title>ASP.NET TEST</title> </head> <body> <form id="form1" runat="server"> <div> <asp:PlaceHolder ID="PlaceHolder1" runat="server" /><br /> <asp:PlaceHolder ID="PlaceHolder2" runat="server" /><br /> <asp:Label runat="server" ID="Label1" /> </div> </form> </body> </html>praseTech
Member
198 Points
51 Posts
Re: ASP.NET dynamic buttons
May 01, 2012 06:07 AM|LINK
I would suggest you to maintain a hidden variable.
Ruchira
All-Star
43068 Points
7045 Posts
MVP
Re: ASP.NET dynamic buttons
May 01, 2012 02:14 PM|LINK
Hello,
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.
My Tech blog | My YouTube ChannelPlease 'Mark as Answer' if this post helps you.