Go Back   CodingForums.com > :: Server side development > ASP.NET

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 04-30-2012, 02:04 PM   PM User | #1
krienas
New to the CF scene

 
Join Date: Apr 2012
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
krienas is an unknown quantity at this point
Help with dynamic buttons

Hello,

I'm new with ASP.NET and I need to create dynamic buttons.
Code:
<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.
krienas is offline   Reply With Quote
Old 04-30-2012, 06:22 PM   PM User | #2
alykins
Senior Coder

 
alykins's Avatar
 
Join Date: Apr 2011
Posts: 1,608
Thanks: 37
Thanked 183 Times in 182 Posts
alykins will become famous soon enough
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.
__________________

I code C hash-tag .Net
Reference: W3C W3CWiki .Net Lib
Validate: html CSS
Debug: Chrome FireFox IE
alykins is offline   Reply With Quote
Old 05-01-2012, 06:00 AM   PM User | #3
krienas
New to the CF scene

 
Join Date: Apr 2012
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
krienas is an unknown quantity at this point
Quote:
Originally Posted by alykins View Post
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).
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>
krienas is offline   Reply With Quote
Old 05-01-2012, 12:02 PM   PM User | #4
alykins
Senior Coder

 
alykins's Avatar
 
Join Date: Apr 2011
Posts: 1,608
Thanks: 37
Thanked 183 Times in 182 Posts
alykins will become famous soon enough
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
__________________

I code C hash-tag .Net
Reference: W3C W3CWiki .Net Lib
Validate: html CSS
Debug: Chrome FireFox IE
alykins is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 06:18 AM.


Advertisement
Log in to turn off these ads.