Event delegate parser

K

Kirill Osipov

Hello everybody.
Can anyone help me? I'm writing a server control which raises events. On
MSDN I've read a lot about events and saw a bunch of samples illustrating
the mechanism of events. Among those samples I've saw a couple of ones on
which built-in ASP.NET controls featured event initialization right from
ASCX/ASPX code. For example:
<asp:Repeater id="rptMyRepeater" OnItemDataBound="OnMyRepeaterDataBind"
runat="server">
....
</asp:Repeater>

The piece of interest is the OnItemDataBound property. The documentation
says that it initializes the ItemDataBound event for the control. Does
anybody know how to implement this behavior so that the name of the event
handler is supplied in the attribute value? Suppose I'm having a control
nmsp:MyControl which raises the TestEvent event. What should I do to make
this syntax work:
<nmsp:MyControl id="ctlMyControl" runat="server"
OnTestEvent="OnMyControlTestEvent" />
?
Any hint/reference/sample will be appreciated
Thanks,
Kirill Osipov
 
D

Derek Harmon

Kirill Osipov said:
<nmsp:MyControl id="ctlMyControl" runat="server"
OnTestEvent="OnMyControlTestEvent" /> : :
Does anybody know how to implement this behavior so that the name of the
event handler is supplied in the attribute value?

All your control needs to do is expose a public event property, and ASP.NET
does the rest automatically.

For instance, your control needs to declare the following delegate type,

public void delegate TestEventHandler( object sender, EventArgs args);

and then inside of your server control's class you need to have a field
whose type corresponds to the delegate, an event property, and an
virtual method that you call from someplace within your control's logic
that actually fires the event at the appropriate time,

private TestEventHandler test;
public event TestEventHandler TestEvent
{
add { this.test += value; }
remove { this.test -= value; }
}
protected virtual void OnTest( EventArgs e)
{
if ( null != this.test )
this.test( this, e);
}

The OnEVENTNAME attribute of a server control can only bind to
the *public* event handlers on the WebForm, however. When the
event handler is private you'll receive the error message that it's
inaccessible due to it's protection level.


Derek Harmon
 

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

No members online now.

Forum statistics

Threads
473,780
Messages
2,569,608
Members
45,248
Latest member
MagdalenaB

Latest Threads

Top