locked
event in Custom control on imagebutton RRS feed

  • Question

  • User1145340372 posted

     Hello,

     I am busy with a custom control and i'm trying to add and event.

    I have a sub what created some imagebuttons dynamic. 

    Top declaration:
    1   
    2        Public Event FolderClick As ImageClickEventHandler
    3   
    4        Protected Overridable Sub OnFolderClick(ByVal sender As Object, ByVal e As ImageClickEventArgs)
    5            RaiseEvent FolderClick(sender, e)
    6        End Sub

      

    Creating the imagebuttons 
    1            For Each dr As DataRow In ds.Tables(0).Rows
    2                _MediaCell = New TableCell
    3                img = New ImageButton
    4                img.ID = dr("folder_id").ToString
    5                img.ImageUrl = "../images/folder.gif"
    6                AddHandler img.Click, AddressOf OnFolderClick
    7                _MediaCell.Controls.Add(img)
    8   
    9                _MediaRow.Cells.Add(_MediaCell)
    10           Next


    now i try to catch the event but nothing is happening:
    1        Protected Sub folderClick(ByVal sender As Object, ByVal e As ImageClickEventArgs) Handles CustomControl.FolderClick
    2            Dim a = sender
    3        End Sub

     When i put breakpoint in de OnFolderClick also nothing happens.
    What am i doing wrong!

    Friday, November 23, 2007 10:10 AM

Answers

  • User-1136466523 posted

    Hi,

    Well, try to get the instance of your "imageButton" from your parent control, and add the event handler after you add the imagebutton into the tabelcell. See the following code snippet which works on my machine:

    Dim ib As New ImageButton()
    Dim tb As New Table()
    Dim tr As New TableRow()
    Dim tc As New TableCell()

    tb.ID = Guid.NewGuid().ToString()
    tr.ID = Guid.NewGuid().ToString()
    tc.ID = Guid.NewGuid().ToString()

    ib.ID = "abc"
    ib.ImageUrl = "../YouImageFile"

    tc.Controls.Add(ib)
    tr.Cells.Add(tc)
    tb.Rows.Add(tr)

    Page.Form.Controls.Add(tb)   -- Add the imagebutton into the tabel.

    Dim ib_obj As ImageButton = DirectCast(tb.FindControl("abc"), ImageButton)
    AddHandler ib_obj.Click, AddressOf ib_obj_Click
      -- add the event handler here.

    /// Eventhandler:

     Private Sub ib_obj_Click(ByVal sender As Object, ByVal e As ImageClickEventArgs)
        Response.Write("This event is fired by the ImageButton from the TableCell")
    End Sub

    When you run it, click on the image, it would show "This event is fired by the ImageButton from the TableCell" which indicates that the event has been fired.

    Thanks.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, November 28, 2007 1:14 AM

All replies

  • User-1136466523 posted

    Hi,

    From your description, it seems that you want to create your imagebutton and bind the event handler to those buttons dynamically, right?

    If so, you may try the following code snippet:

    For Each dr As DataRow In ds.Tables(0).Rows
                    _MediaCell = New TableCell
                    Dim img As New ImageButton()                
                    img.ID = dr("folder_id").ToString
                    img.ImageUrl = "../images/folder.gif"
                    AddHandler img.Click, AddressOf img_Click
                    _MediaCell.Controls.Add(img)
                    _MediaRow.Cells.Add(_MediaCell)
    Next

    Private Sub imgb_Click(ByVal sender As Object, ByVal e As ImageClickEventArgs)
        Response.Write("This image has been clicked!")
    End Sub

    Thanks.

    Monday, November 26, 2007 9:06 PM
  • User1145340372 posted

    thanx for your answer but this doesnt work.

    When run the control, it doesnt reach  the break point I placed in de imgb_Click sub.

     

     

    Tuesday, November 27, 2007 2:33 AM
  • User-1136466523 posted

    Hi,

    Well, try to get the instance of your "imageButton" from your parent control, and add the event handler after you add the imagebutton into the tabelcell. See the following code snippet which works on my machine:

    Dim ib As New ImageButton()
    Dim tb As New Table()
    Dim tr As New TableRow()
    Dim tc As New TableCell()

    tb.ID = Guid.NewGuid().ToString()
    tr.ID = Guid.NewGuid().ToString()
    tc.ID = Guid.NewGuid().ToString()

    ib.ID = "abc"
    ib.ImageUrl = "../YouImageFile"

    tc.Controls.Add(ib)
    tr.Cells.Add(tc)
    tb.Rows.Add(tr)

    Page.Form.Controls.Add(tb)   -- Add the imagebutton into the tabel.

    Dim ib_obj As ImageButton = DirectCast(tb.FindControl("abc"), ImageButton)
    AddHandler ib_obj.Click, AddressOf ib_obj_Click
      -- add the event handler here.

    /// Eventhandler:

     Private Sub ib_obj_Click(ByVal sender As Object, ByVal e As ImageClickEventArgs)
        Response.Write("This event is fired by the ImageButton from the TableCell")
    End Sub

    When you run it, click on the image, it would show "This event is fired by the ImageButton from the TableCell" which indicates that the event has been fired.

    Thanks.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, November 28, 2007 1:14 AM
  • User1145340372 posted

    thanx again but this issnt possible.

    It is a custom control and i cant directy add the controls to the page. because after creating the table some other actions have to be performed
    And second it can be 1 image but there can also be 100.

    I t has to be possible  but i am doing something wrong and i cant find out what it is

    Friday, November 30, 2007 8:52 AM
  • User-1136466523 posted

    Hi,

    It is a custom control and i cant directy add the controls to the page. because after creating the table some other actions have to be performed
    And second it can be 1 image but there can also be 100.

    If you have more than 100 controls, you still have to add it programatically, right? Then, after you have added all the controls onto page, which indicates that the all the controls have been added to the page's control hirarchical, then try to get the instance of the img control from its parent, and bind the corresponding events to them although it would be a huge work. If you have other good ways, i'll be appreciate it if you can share it with us.

    Thanks.

    Sunday, December 2, 2007 9:23 PM