Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Apr 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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.

  • #2
    Senior Coder alykins's Avatar
    Join Date
    Apr 2011
    Posts
    1,897
    Thanks
    46
    Thanked 202 Times in 201 Posts
    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

  • #3
    New to the CF scene
    Join Date
    Apr 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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>

  • #4
    Senior Coder alykins's Avatar
    Join Date
    Apr 2011
    Posts
    1,897
    Thanks
    46
    Thanked 202 Times in 201 Posts
    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


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •