...

View Full Version : Help with dynamic buttons



krienas
04-30-2012, 03:04 PM
Hello,

I'm new with ASP.NET and I need to create dynamic buttons.

<script runat="server">
Sub submit(Source As Object, e As EventArgs)
button2.Visible = True
lbl1.Visible = True
button3.Visible = True
End Sub
</script>
<script runat="server">
Sub submit1(Source As Object, e As EventArgs)
lbl1.Text += 1
End Sub
</script>
<script runat="server">
Sub submit2(Source As Object, e As EventArgs)
button2.Visible = False
lbl1.Visible = False
button3.Visible = False
End Sub
</script>


<html>
<body style="height: 184px">

<form id="Form1" runat="server">
<asp:Button id="button1" Text="ADD" runat="server" OnClick="submit" />
<p>
<asp:Button id="button2" Text="COUNT" runat="server" visible="false" OnClick="submit1" />
</p>
<p>
<asp:Label id="lbl1" Text=0 runat="server" Visible=false />
&nbsp&nbsp&nbsp&nbsp
<asp:Button id="button3" Text="REMOVE" runat="server" visible="false" OnClick="submit2" />
</p>
</form>

</body>
</html>
I made this code but it's not really a dynamic, because it uses visible true/false commands.
Can any one help me to make them dynamic.
Thank you.

alykins
04-30-2012, 07:22 PM
what is it that you are trying to make 'dynamic'? are you just trying to do one button call? you can do that by giving it command arguments and casting the object as a button and switching on the argument- if you are talking about on the client end, simple answer is you can't without javascript, in which case this would move to either JS or AJAX section.

krienas
05-01-2012, 07:00 AM
what is it that you are trying to make 'dynamic'? are you just trying to do one button call? you can do that by giving it command arguments and casting the object as a button and switching on the argument- if you are talking about on the client end, simple answer is you can't without javascript, in which case this would move to either JS or AJAX section.
By "dynamic" I mean that I don't have to put <asp:Button> to my form. Add button created dynamically on page load, Count and Remove on Add click and so on.
Any way I managed to get buttons created dynamically but now I bumped into another problem. I can't seem to get Count and Remove buttons working. They just removes themselves (it's fine with Remove button but Count should add +1 to label value).

<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>

alykins
05-01-2012, 01:02 PM
from the looks of the code every button that you create 'dynamically' is named btn and has a handle of btn_Click- how are those new buttons ever supposed to know that they are to be btn1 or btn2? you need to modify the creation and add the number to make it unique- they must be getting something because objects with the same name should thrown an error- idk I've never created objects on the fly and named them the same- I would think you should get a runtime error



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum