Results 1 to 9 of 9

Thread: dynamic event handling within a custom control

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jul 2006
    Posts
    16

    dynamic event handling within a custom control

    Hi,

    I have a custom control which contains (amongst other things) an imageButton.

    I'm adding a handler to the "click" event of the imageButton however when the button is clicked the handler routine is not called.

    Can anyone tell me where I am going wrong with this? should I be handling the click event in some other way?

    here is my code:


    Code:
    Imports Microsoft.VisualBasic
    Imports System.Web.UI.WebControls
    Imports System.Web.UI
    
    Namespace company.web.media
        Public Class movieThumbnail
    
    
            Inherits WebControl
    
            Private _duration As String
            Private _title As String
            Private _imageURI As String
            Private _movieID As Integer
            Private img As New ImageButton
    
    
            Public Property imageURI() As String
                Get
                    Return Me._imageURI
                End Get
                Set(ByVal value As String)
                    Me._imageURI = value
                End Set
            End Property
            Public Property title() As String
                Get
                    Return Me._title
                End Get
                Set(ByVal value As String)
                    Me._title = value
                End Set
            End Property
    
            Public Property duration() As String
                Get
                    Return Me._duration
                End Get
                Set(ByVal value As String)
                    Me._duration = value
                End Set
            End Property
            Public Property movieID() As Integer
                Get
                    Return Me._movieID
                End Get
                Set(ByVal value As Integer)
                    Me._movieID = value
                End Set
            End Property
            Protected Overrides Sub RenderContents(ByVal writer As System.Web.UI.HtmlTextWriter)
    
                Dim table As New Table
    
    
                'setup generic styling
                table.Font.Name = "verdana"
                table.BackColor = System.Drawing.Color.FromArgb(25, 25, 25, 25)
                table.ForeColor = Drawing.Color.LightGray
                table.Width = 107
    
                'add three rows
                table.Rows.Add(New TableRow)
                table.Rows.Add(New TableRow)
                table.Rows.Add(New TableRow)
    
                'add image to first row
                table.Rows(0).Cells.Add(New TableCell)
                table.Rows(0).Cells(0).ColumnSpan = 2
                img.ImageUrl = Me._imageURI
                img.Height = 100
                img.Width = 100
                img.Style.Add("cursor", "pointer")
    
                AddHandler img.Click, AddressOf imgClick
    
                table.Rows(0).Cells(0).Controls.Add(img)
    
                'add title to second row
                table.Rows(1).Cells.Add(New TableCell)
                table.Rows(1).Cells(0).Text = Me._title
                table.Rows(1).Cells(0).Font.Size = FontUnit.Point(8)
    
                'add duration to third row
                table.Rows(2).Cells.Add(New TableCell)
                table.Rows(2).Cells(0).Text = Me._duration
                table.Rows(2).Cells(0).Font.Size = FontUnit.Point(10)
    
                'render control
                table.RenderControl(writer)
    
            End Sub
    
    
            Private Sub imgClick(ByVal sender As Object, ByVal e As ImageClickEventArgs)
                MsgBox(Me._movieID)
            End Sub
    
        End Class
    
    
    End Namespace


    and the markup simply:

    Code:
    <l11:movieThumbnail runat="server" duration="14:42" title="move title" imageURI="http://www.google.co.uk/logos/newton10-tree.jpg" />

  2. #2
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: dynamic event handling within a custom control

    Hey,

    To start with, as a general rule, it is bad practice to use a MsgBox for debugging purposes. Use debug/trace writes instead.

    Have you set a break point in the click event? Does it ever get hit?

    Gary

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Jul 2006
    Posts
    16

    Re: dynamic event handling within a custom control

    No, the breakpoint is not hit within the triggered routine (imgClick()) but the breakpoint is hit on the line which adds the handler.

  4. #4
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: dynamic event handling within a custom control

    Hey,

    I believe I am correct in saying that the AddHandler call is being placed in the wrong place. This needs to be done earlier in the page life cycle, otherwise it will never get hit.

    It has been a while, but I would have thought in the CreateChildControls overridden method.

    Gary

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Jul 2006
    Posts
    16

    Re: dynamic event handling within a custom control

    I have overridden the createChildControls() routine as suggested and added in the line which adds the handler (see code below) however I am still getting the same result. The addhandler line does ger hit however when the button is clicked the imgClick routine does not get hit

    Code:
          Protected Overrides Sub CreateChildControls()
                MyBase.CreateChildControls()
                AddHandler img.Click, AddressOf imgClick
            End Sub
            Protected Overrides Sub RenderContents(ByVal writer As System.Web.UI.HtmlTextWriter)
    
                Dim table As New Table
    
    
                'setup generic styling
                table.Font.Name = "verdana"
                table.BackColor = System.Drawing.Color.FromArgb(25, 25, 25, 25)
                table.ForeColor = Drawing.Color.LightGray
                table.Width = 107
    
                'add three rows
                table.Rows.Add(New TableRow)
                table.Rows.Add(New TableRow)
                table.Rows.Add(New TableRow)
    
                'add image to first row
                table.Rows(0).Cells.Add(New TableCell)
                table.Rows(0).Cells(0).ColumnSpan = 2
                img.ImageUrl = Me._imageURI
                img.Height = 100
                img.Width = 100
                img.Style.Add("cursor", "pointer")
    
    
    
                table.Rows(0).Cells(0).Controls.Add(img)
    
                'add title to second row
                table.Rows(1).Cells.Add(New TableCell)
                table.Rows(1).Cells(0).Text = Me._title
                table.Rows(1).Cells(0).Font.Size = FontUnit.Point(8)
    
                'add duration to third row
                table.Rows(2).Cells.Add(New TableCell)
                table.Rows(2).Cells(0).Text = Me._duration
                table.Rows(2).Cells(0).Font.Size = FontUnit.Point(10)
    
                'render control
                table.RenderControl(writer)
    
            End Sub
    any other ideas?

  6. #6
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: dynamic event handling within a custom control

    Hey,

    I would suggest that you start with a known working example, i.e:

    http://www.dotnetjohn.com/articles.aspx?articleid=126

    Get that up and running, then start to edit it to suit your needs.

    Gary

  7. #7
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: dynamic event handling within a custom control

    Recreate those controls, or at least the img, in the Init event of your ASCX. Right now, when the postback occurs, the dynamic control's viewstate isn't reassigned. If you do it in the Init, the viewstate gets assigned, the page then realizes that the event needs to be raised.

    More Info

  8. #8

    Thread Starter
    Junior Member
    Join Date
    Jul 2006
    Posts
    16

    Re: dynamic event handling within a custom control

    Hi,

    Thanks for your help mendhak and gep13. Implementing iNamingContainer fixed the issue (maybe someone could explain why?). I got this by comparing the example you sent gep13.

    the solution you suggested mendhak also worked, creating the control in the init routine instead of the overidden createChildControls() routine.

    Thanks again

    Chris V

  9. #9
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: dynamic event handling within a custom control

    INamingContainer ensures that the names are unique and I think it maintains the names across postbacks too (so that viewstate can be assigned to it), which is effectively the same thing. I haven't used it to maintain viewstate before though, I usually use Init.

Tags for this Thread

Posting Permissions

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



Click Here to Expand Forum to Full Width