|
-
Jan 4th, 2010, 04:44 PM
#1
Thread Starter
Junior Member
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" />
-
Jan 5th, 2010, 03:15 AM
#2
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
-
Jan 5th, 2010, 06:25 AM
#3
Thread Starter
Junior Member
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.
-
Jan 5th, 2010, 06:43 AM
#4
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
-
Jan 5th, 2010, 08:10 AM
#5
Thread Starter
Junior Member
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?
-
Jan 5th, 2010, 08:22 AM
#6
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
-
Jan 7th, 2010, 02:35 PM
#7
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
-
Jan 8th, 2010, 05:34 PM
#8
Thread Starter
Junior Member
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
-
Jan 8th, 2010, 05:36 PM
#9
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|