The previous article describes the basic concepts of implementing custom server control events. This article will explain how to capture the postback event through a typical example.
1. To achieve the capture of the postback event
If the server control needs to capture a postback event from the client and wants to customize the server-side event handling logic for the postback event, the control must implement the System.Web.UI.IPostBackEventHandler interface. The interface definition is listed below.
public interface IPostBackEventHandler
{
void RaisePostBackEvent(string eventArgument);
}
As shown in the previous code, the IPostBackEventHandler interface includes only one member method RaisePostBackEvent. This method enables the server control to handle the event that is raised when the form is sent to the server, whose parameter eventargument represents an optional event parameter to pass to the event handler. In the RaisePostBackEvent method, developers can implement the logic that executes during server control postback. In general, the RaisePostBackEvent method raises one or more server-side events. The following code fragment shows the RaisePostBackEvent implementation that raised the Click event on the server.
public void RaisePostBackEvent(String eventArgument)
{
OnClick(EventArgs.Empty);
}
The implementation of the catch-return event is not just to enable the server control class to implement the IPostBackEventHandler interface and implement the interface member method. Developers also need to be aware of implementing other content. The following is a list of three key points in the process of capturing a postback event.
First, and most importantly, the custom server control class must implement the IPostBackEventHandler interface and implement the interface member RaisePostBackEvent method. This process has been described above.
Second, assign UniqueID to the control.
Defining the Name property value of the control that causes the postback event is UniqueID, which is one of the keys to the correct implementation of the RaisePostBackEvent method. When a postback is raised, the page framework searches for the sent content and determines whether the name of the sending object corresponds to the UniqueID of the server control implementing IPostBackEventHandler. If it corresponds, the page frame invokes the RaisePostBackEvent method on the control. The point here is to require developers to assign UniqueID to the control's Name property in the rendering logic. A simple code example is listed below.
protected override void Render(HtmlTextWriter output)
{
output.Write("<INPUT TYPE=submit name="+this.UniqueID+"Value='Click Me' />");
}
As shown in the code above, a button is rendered in the control rendering method render with the Name property value of UniqueID. Capturing a postback event can be implemented correctly only if UniqueID is assigned to the Name property of the control that caused the postback.
Third, implement the event attribute structure.
The event property structure is an optimized way of event implementation. Before introducing, let's take a look at the common way control event implementations are implemented. The specific code is shown below.
......
public class WebCustomControl:WebControl,IPostBackEventHandler{
//声明Click事件委托
public event EventHandler Click;
//实现RaisePostBackEvent方法
void IPostBackEventHandler.RaisePostBackEvent(string eventArgument) {
OnClick(EventArgs.Empty);
}
//定义OnClick事件处理程序
protected virtual void OnClick(EventArgs e) {
if(Click != null) { Click(this,e); }
}
......
}