Custom Controls that Raise Events
-
Implement or choose the appropriated event data class
-
Declare or choose the appropriated event delegate class
-
Enable you custom control to raise the event
-
If your custom control need the data posted back, then you have to implement IpostBackDataHandler interface to get data to create the event data class defined previously. IpostBackDataHandler has two functions: IPostBackDataHandler.LoadPostData and IPostBackDataHandler.RaisePostDataChangedEvent.The first function get the data you need, and return a boolean which means whether the data has changed. If the data has changed, then the next function- IPostBackDataHandler.RaisePostDataChangedEvent will be invoked.
-
Your custom control must implement IpostBackEventHandler interface which ha s only one function: IPostBackEventHandler.RaisePostBackEvent. there is a trick here, the control triggled the event must has same UniqueId and Name, and because UniqueId is generated By the compiler,so you must set the name attribute of the control to make it same as UniqueId.
-
Tips:
-
Event can only by invoked in the function of the class who define it, you cann’t invoke the Event outside of the class. Typical situation as follows:
public class CustomControl: IPostBackEventHandler2
{3
//event field4
public event ValidatingEventHandler ValidatingEventHandler;5

6
// function to invoke the ValidatingEventHandler,event only7
// can be invoked in the procedure of the class8
protected virtual void OnValidatingEventHandler(EventArgs e)9
{10
ValidatingEventHandler(this,e);11
}12
}
-
If you want a class to implements an interface, then you find you need some subclass inherits from the class, and the subclasses has its own implementation about the interface. So, the solution is you first need to implement a protected virtual method for each method of the interface and a protected property for each property of the Interface. The method must have the same signature as its respective interface method. Example as follows:
Code

浙公网安备 33010602011771号