Subscribing to event from Template Column

T

Tina

I have an ascx control in the ItemTemplate of a Template Column in a
datagrid. The control (myControl) raises an event.

I'm trying to figure out how I can subscribe to the event. It can't be done
at design time because myControl is not one of the recognized controls. At
run time I would expect to use AddHandler but I can't seem to reference
myControl.

How can I subscribe to the event?

Thanks,
T
 
G

Guest

Hi Tina,

If your control is simply a composite control that is placed declaratively
on the page at design time then you can add the event handler within the ascx
codebehind file.

Alternatively, you can use event delegates to expose events out of your ascx
to its NamingContainer (in your case it is the DataGridItem) as I did in this
example: http://www.societopia.net/Samples/DataGridEventDelegates.aspx
 
T

Tina

I guess I'm going to have to learn c# if for nothing other than to follow
the examples....

in VB to handle an event we .....
AddHandler myControl.TimerEvent, AddressOf me.HandleTimerEvent
..
..
Protected Sub HandleTimerEvent(byval o as Object, byval e as TimerArgs)
.. code to handle event is here...
end sub

How would the AddHandler code look in the case where myControl is in
DataGrid1?
 
G

Guest

Hi Tina,

For you to be able to handle an event (e.g. TimerEvent) raised by your
control it has to define a delegate method that you can use when placing that
control in the DataGrid. If it does that then you can handle that event by
specifying a function for the delegate method (e.g. if your event is named
TimerEvent, you should design your control to expose a method named
OnTimerEvent)

I created here
http://www.societopia.net/Samples/DataGrid_ChildControlsEvents.aspx a sample
in VB.Net where I designed a user control (in an ascx file) that raises an
event (I called it CustomEvent1) by exposing a delegate method called
“OnCustomEvent1â€. I then included that control in a TemplateColumn on the
DataGrid and wired the delegate function named OnCustomEvents to an event
handler within the page class codebehind.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,480
Members
44,900
Latest member
Nell636132

Latest Threads

Top