Custom Event Handler

  • Thread starter Thread starter SDF
  • Start date Start date
S

SDF

Hello,

I need to create and handle my own Events complete with my own EventArgs.

Default.aspx has an HTML Button that opens Page2.aspx in a new frame, I need
the user to set some params and then have Default.aspx post back and do
something with the params. I am using window.opener to get Default.aspx to
postback. The problem is creating my own Events to capture. I *think* I
need to create a class that inherits from Control - I won't have a visual
appearance to my control, I just need to define my own Events with my own
EventArgs - and I need to learn how to specify which one of my events to
handle on PostBack.

Are their tutorials on this? I have a few books but they do not go into
details on this. I'm sure this has been done before.

Any comments greatly appreciated.

Scott
 
To create your own event on a control

1 / Defines the class of the event argument :
class MyEventArgs : EventArgs

2 / Defines the prototype of your event as a delegue :
public delegate void MyEventHandlerDelegate (object sender, MyEventArgs e);

3 / Inherits from the desired control (the design-time rendering will be the
same that the control that you inherit) :
class MyButton : System.Web.UI.Button

4 / Declares the event in the MyButton class
public event MyEventHandlerDelegate CustomEvent;

5 / Create the method that raise the event
protected virtual void OnCustomEvent (MyEventArgs e)
{
if (CustomEvent != null)
CustomEvent(this, e);
}

6 / Implements the IPostBackEventHandler (this interface allows your control
to raise event)
public class MyButton : Button, IPostBackEventHandler
{
...
private IPostBackEventHandler.RaisePostBackEvent(string eventArgument)
{
OnCustomEvent (new MyEventArgs(...));
}
}

finished :)
 
Back
Top