I created a custom control that contains a table of labels put on a panel, I build the control in a separate vb file which I add as a DLL to my web application.
I want that my custom control will react to a click event (just like a button) but I don't know how to develope an
onclick event to it.
I tried to put the labels table on a button and to put it on the main control panel but the button hides the labels.
Does anybody know how can I generate an onclick event to my custom control?
I don't understand exactly what you want to do. I just give you an example about Events. You must implement IPostBackEventHandler.
If you add javascript, 'onclick' event for example, you use Page.ClientScript.GetPostBackEventReference.
RaisePostBackEvent can occur event Click. I'll try convert an example in c# to vb. In this code, when you click in HtmlAnchor, a Label change (increment a number), and fire Click event.
1 Public Class MyControl
2 Inherits CompositeControl
3
4 Implements IPostBackEventHandler
5
6 Protected Overrides Sub CreateChildControls()
7 MyBase.CreateChildControls
8 Me._bouton = New HtmlAnchor
9 Me._bouton.InnerText = "Add"
10 Me._bouton.HRef = "#"
11 Me._bouton.ID = ("Button" & Me.UniqueID)
12 Me._bouton.Attributes.Item("OnClick") = Me.Page.ClientScript.GetPostBackEventReference(Me, "Click")
13 Me._label = New Label
14 Me._label.Text = Me._compteur.ToString
15 End Sub
16
17 Public Event Click As EventHandler
18
19 Protected Overridable Sub OnClick(ByVal e As EventArgs)
20 If (Not Me.Click Is Nothing) Then
21 Me.Click.Invoke(Me, e)
22 End If
23 End Sub
24
25 Protected Overrides Sub Render(ByVal output As HtmlTextWriter)
26 Me._bouton.RenderControl(output)
27 Me._label.RenderControl(output)
28 End Sub
29
30 Private Sub System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(ByVal eventArgument As String) Implements IPostBackEventHandler.RaisePostBackEvent
31 If (eventArgument Is "Click") Then
32 Me._compteur += 1
33 Me.OnClick(EventArgs.Empty)
34 End If
35 End Sub
36
37
38
39 Private Property _compteur As Integer
40 Get
41 Return IIf((Not Me.ViewState.Item("Compteur") Is Nothing), Integer.Parse(Me.ViewState.Item("Compteur").ToString), 0)
42 End Get
43 Set(ByVal value As Integer)
44 Me.ViewState.Item("Compteur") = value
45 End Set
46 End Property
47
48 Private _bouton As HtmlAnchor
Private _label As Label
49
50 End Class
yairt
Member
64 Points
24 Posts
Create an onclick event to a custom control
Oct 09, 2006 07:41 AM|LINK
Hello everybody
I created a custom control that contains a table of labels put on a panel, I build the control in a separate vb file which I add as a DLL to my web application.
I want that my custom control will react to a click event (just like a button) but I don't know how to develope an onclick event to it.
I tried to put the labels table on a button and to put it on the main control panel but the button hides the labels.
Does anybody know how can I generate an onclick event to my custom control?
thanks
tkfe
Member
115 Points
23 Posts
MVP
Re: Create an onclick event to a custom control
Oct 09, 2006 12:18 PM|LINK
Hello
I don't understand exactly what you want to do. I just give you an example about Events. You must implement IPostBackEventHandler.
If you add javascript, 'onclick' event for example, you use Page.ClientScript.GetPostBackEventReference. RaisePostBackEvent can occur event Click. I'll try convert an example in c# to vb. In this code, when you click in HtmlAnchor, a Label change (increment a number), and fire Click event.
1 Public Class MyControl 2 Inherits CompositeControl 3 4 Implements IPostBackEventHandler 5 6 Protected Overrides Sub CreateChildControls() 7 MyBase.CreateChildControls 8 Me._bouton = New HtmlAnchor 9 Me._bouton.InnerText = "Add" 10 Me._bouton.HRef = "#" 11 Me._bouton.ID = ("Button" & Me.UniqueID) 12 Me._bouton.Attributes.Item("OnClick") = Me.Page.ClientScript.GetPostBackEventReference(Me, "Click") 13 Me._label = New Label 14 Me._label.Text = Me._compteur.ToString 15 End Sub 16 17 Public Event Click As EventHandler 18 19 Protected Overridable Sub OnClick(ByVal e As EventArgs) 20 If (Not Me.Click Is Nothing) Then 21 Me.Click.Invoke(Me, e) 22 End If 23 End Sub 24 25 Protected Overrides Sub Render(ByVal output As HtmlTextWriter) 26 Me._bouton.RenderControl(output) 27 Me._label.RenderControl(output) 28 End Sub 29 30 Private Sub System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(ByVal eventArgument As String) Implements IPostBackEventHandler.RaisePostBackEvent 31 If (eventArgument Is "Click") Then 32 Me._compteur += 1 33 Me.OnClick(EventArgs.Empty) 34 End If 35 End Sub 36 37 38 39 Private Property _compteur As Integer 40 Get 41 Return IIf((Not Me.ViewState.Item("Compteur") Is Nothing), Integer.Parse(Me.ViewState.Item("Compteur").ToString), 0) 42 End Get 43 Set(ByVal value As Integer) 44 Me.ViewState.Item("Compteur") = value 45 End Set 46 End Property 47 48 Private _bouton As HtmlAnchor Private _label As Label 49 50 End ClassFrederic Melantois