Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. How to handle events of dynamically created buttons???

How to handle events of dynamically created buttons???

Scheduled Pinned Locked Moved ASP.NET
asp-netdesignjsonhelptutorial
8 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    Backyardflying
    wrote on last edited by
    #1

    I'm dynamically updating a web forms table control (tblPics). I'm adding a web forms ImageControl to each cells. The problem is that the function "DisplayImage" is never called when I press one of the images (see Addhandler in the code). The rest works perfectly. Thanks. Here's a part of my code: Private Sub Page_Load ... If Not Page.IsPostBack Then InitPage() End If End Sub Private Sub InitPage() Dim _ImageButton As ImageButton For j = 0 To _Rows - 1 Dim r As New TableRow Dim i As Integer For i = 0 To _Cols - 1 Dim c As New TableCell _ImageButton = New ImageButton _ImageButton.ID = "IB" & Ind.ToString _ImageButton.CommandName = "Display" _ImageButton.CommandArgument = _FileList(Ind) _ImageButton.ImageUrl = _FileList(Ind) AddHandler _ImageButton.Command, AddressOf DisplayImage c.HorizontalAlign = HorizontalAlign.Center c.BorderStyle = BorderStyle.Double c.Controls.Add(_ImageButton) r.Cells.Add(c) Ind = Ind + 1 If Ind = _FileList.Count Then bExit = True Exit For End If Next i tblPics.Rows.Add(r) If bExit Then Exit For End If Next j tblPics.BorderStyle = BorderStyle.None end sub Private Sub DisplayImage(ByVal sender As System.Object, ByVal e As System.Web.UI.WebControls.CommandEventArgs) lblTitle.Text = "test" End Sub

    D A 2 Replies Last reply
    0
    • B Backyardflying

      I'm dynamically updating a web forms table control (tblPics). I'm adding a web forms ImageControl to each cells. The problem is that the function "DisplayImage" is never called when I press one of the images (see Addhandler in the code). The rest works perfectly. Thanks. Here's a part of my code: Private Sub Page_Load ... If Not Page.IsPostBack Then InitPage() End If End Sub Private Sub InitPage() Dim _ImageButton As ImageButton For j = 0 To _Rows - 1 Dim r As New TableRow Dim i As Integer For i = 0 To _Cols - 1 Dim c As New TableCell _ImageButton = New ImageButton _ImageButton.ID = "IB" & Ind.ToString _ImageButton.CommandName = "Display" _ImageButton.CommandArgument = _FileList(Ind) _ImageButton.ImageUrl = _FileList(Ind) AddHandler _ImageButton.Command, AddressOf DisplayImage c.HorizontalAlign = HorizontalAlign.Center c.BorderStyle = BorderStyle.Double c.Controls.Add(_ImageButton) r.Cells.Add(c) Ind = Ind + 1 If Ind = _FileList.Count Then bExit = True Exit For End If Next i tblPics.Rows.Add(r) If bExit Then Exit For End If Next j tblPics.BorderStyle = BorderStyle.None end sub Private Sub DisplayImage(ByVal sender As System.Object, ByVal e As System.Web.UI.WebControls.CommandEventArgs) lblTitle.Text = "test" End Sub

      D Offline
      D Offline
      darkelv
      wrote on last edited by
      #2

      You'll have to add (+=) the onclick event when you create the button and destroy (-=) it when you remove the button.

      B 1 Reply Last reply
      0
      • D darkelv

        You'll have to add (+=) the onclick event when you create the button and destroy (-=) it when you remove the button.

        B Offline
        B Offline
        Backyardflying
        wrote on last edited by
        #3

        I don't really understand what you're explaining ... can you give an example of what I should be using in my code. Thanks.

        D 1 Reply Last reply
        0
        • B Backyardflying

          I don't really understand what you're explaining ... can you give an example of what I should be using in my code. Thanks.

          D Offline
          D Offline
          darkelv
          wrote on last edited by
          #4

          I am using C# as an example. When you create a new button. Button button = new Button(); button.Click += new System.EventHandler(button_OnClick); button_Onclick will be your click handler. Before you remove the button, you should remove the event handler by using the button.Click -= new System.EventHandler(button_OnClick);

          B 1 Reply Last reply
          0
          • D darkelv

            I am using C# as an example. When you create a new button. Button button = new Button(); button.Click += new System.EventHandler(button_OnClick); button_Onclick will be your click handler. Before you remove the button, you should remove the event handler by using the button.Click -= new System.EventHandler(button_OnClick);

            B Offline
            B Offline
            Backyardflying
            wrote on last edited by
            #5

            button.click .... you cannot do that in VB.NET! I'm using this but the displayimage event handler never fires: AddHandler _ImageButton.Command, AddressOf DisplayImage

            D 1 Reply Last reply
            0
            • B Backyardflying

              button.click .... you cannot do that in VB.NET! I'm using this but the displayimage event handler never fires: AddHandler _ImageButton.Command, AddressOf DisplayImage

              D Offline
              D Offline
              darkelv
              wrote on last edited by
              #6

              I didn't realise the line is adding the event handler in vb.net. :) Does the control has AutoPostback=true and the page is posted back to server when you click the button? -- modified at 12:34 Saturday 25th February, 2006 Sorry, 'Autopostback=true' should be 'runat="server"'

              B 1 Reply Last reply
              0
              • D darkelv

                I didn't realise the line is adding the event handler in vb.net. :) Does the control has AutoPostback=true and the page is posted back to server when you click the button? -- modified at 12:34 Saturday 25th February, 2006 Sorry, 'Autopostback=true' should be 'runat="server"'

                B Offline
                B Offline
                Backyardflying
                wrote on last edited by
                #7

                i'v added to my code: _ImageButton.EnableViewState = True _ImageButton.CausesValidation = True but it still does not work. Other buttons on the same page work perfectly. Thanks. -- modified at 12:42 Saturday 25th February, 2006

                1 Reply Last reply
                0
                • B Backyardflying

                  I'm dynamically updating a web forms table control (tblPics). I'm adding a web forms ImageControl to each cells. The problem is that the function "DisplayImage" is never called when I press one of the images (see Addhandler in the code). The rest works perfectly. Thanks. Here's a part of my code: Private Sub Page_Load ... If Not Page.IsPostBack Then InitPage() End If End Sub Private Sub InitPage() Dim _ImageButton As ImageButton For j = 0 To _Rows - 1 Dim r As New TableRow Dim i As Integer For i = 0 To _Cols - 1 Dim c As New TableCell _ImageButton = New ImageButton _ImageButton.ID = "IB" & Ind.ToString _ImageButton.CommandName = "Display" _ImageButton.CommandArgument = _FileList(Ind) _ImageButton.ImageUrl = _FileList(Ind) AddHandler _ImageButton.Command, AddressOf DisplayImage c.HorizontalAlign = HorizontalAlign.Center c.BorderStyle = BorderStyle.Double c.Controls.Add(_ImageButton) r.Cells.Add(c) Ind = Ind + 1 If Ind = _FileList.Count Then bExit = True Exit For End If Next i tblPics.Rows.Add(r) If bExit Then Exit For End If Next j tblPics.BorderStyle = BorderStyle.None end sub Private Sub DisplayImage(ByVal sender As System.Object, ByVal e As System.Web.UI.WebControls.CommandEventArgs) lblTitle.Text = "test" End Sub

                  A Offline
                  A Offline
                  Anish Gopi
                  wrote on last edited by
                  #8

                  Hai Try out this one

                  Private Sub Page_Load
                  InitPage()
                  End sub

                  While working with dynamic controls you need to build controls on each post back. Its better to build dynamic controls in Page_Init

                  Private Sub Page_Init ....
                  InitPage()
                  End Sub

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups