asp.net2.0 server control's capture postback event
Source: Internet
Author: User
Asp.net| Server | Control 1. To implement a capture 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.
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{
Declaring the Click event Delegate
public event EventHandler Click;
Implement RaisePostBackEvent method
void Ipostbackeventhandler.raisepostbackevent (String eventargument) {
OnClick (Eventargs.empty);
}
Defining the OnClick event handler
protected virtual void OnClick (EventArgs e) {
if (click!= null) {click (this,e);}
}
......
}
In the above code, there are three key elements related to the definition of the event: first, define the Click event delegate; second, the control class implements the IPostBackEventHandler interface, in which when the interface member method is implemented RaisePostBackEvent process, The event handler onclick is defined, and the OnClick event handler is implemented. The above implementation method is simple and easy to use, however, there is a disadvantage, namely inefficient execution. In particular, when multiple events are raised in a class, the overhead is increased, the amount of server resources is wasted, and the running efficiency is reduced.
To solve the above problem, an optimized event implementation Method-event attribute structure is described below. The structure uses the System.ComponentModel.EventHandlerList class, which provides a simple list of delegates. By using the relevant methods provided by this class, developers can flexibly manipulate the list of event-handler delegates for the control. For example, the Click event in the control uses the event property structure as follows:
Before you define an event property structure, you first need to define the Click event Delegate object. Because each event is created only once, it needs to be declared static and read-only. The event handler delegate list is then manipulated through the AddHandler, RemoveHandler methods in the property structure. When the page calls the Click event, it adds or deletes handlers to the EventHandlerList collection of the control. Because this implementation method is more efficient than the common implementation method in the declaration process of multiple events, it is a highly recommended method.
In addition, in the implementation of the OnClick method, when an event property is used, the delegate must be retrieved from the EventHandlerList and converted to the EventHandler type.
Please note that the event attribute structure does not apply to vb.net languages, but only in languages such as C #.
2. Typical applications
Realistically speaking, the above theory of capturing the return event is difficult to understand for readers who have never implemented server control events. To do this, this section uses a typical example to illustrate the implementation method of capturing a postback event.
This example implements a custom server control Webcustomcontrol. The control, although rendered as a button appearance, is not inherited from the button class. When the button is clicked, the control causes a postback, the server side automatically captures the postback click event, and the Click event is raised to perform the corresponding event handler. The following are the source code codes implemented by the server control:
Using System;
Using System.Collections.Generic;
Using System.ComponentModel;
Using System.Text;
Using System.Web;
Using System.Web.UI;
Using System.Web.UI.WebControls;
namespace webcontrollibrary{[DefaultEvent ("click")]
[ToolBoxData ("<{0}:webcustomcontrol runat=server></{0}:webcustomcontrol>")]
public class Webcustomcontrol:webcontrol, IPostBackEventHandler {
Define a Click event delegate object
private static readonly Object Eventclick = new Object ();
To implement the Click event property
[Description ("Click event Properties"), Category ("Action")]
Public event EventHandler Click {
Add {
Events.addhandler (Eventclick, value);
}
Remove {
Events.removehandler (Eventclick, value);
}
}
Overriding the control rendering method rendercontents
Figure 1 and Figure 2 below are the applied effect graphs.
Figure1 page initialization effect Figure 2 Click on the effect of the button
In addition, there is a property autopostback that is closely related to capturing a postback event. This property is used for automatic postback settings for controls, which are included in many standard server controls. It is important for developers to define this property in a server control that you create, and you need to consider trade-offs based on the functionality requirements of your control. The key code for this property is implemented as follows:
Defining Properties AutoPostBack
public bool autopostback{
set {
This._autopostback = value;
}
get {
return this._autopostback;
}
}
Add the Page.GetPostBackEventReference () method to the Render method
protected override void Render (HtmlTextWriter output) {
......
if (this. AutoPostBack) {
Writer. WriteAttribute ("ontextchanged", "JavaScript:" + page.getpostbackeventreference (this));
}
......
}
It is known from the above code that the implementation of the AutoPostBack attribute is focused on the application of Page.GetPostBackEventReference method. This method gets a reference to the client script function, which causes the server to send it back to the page and returns a string representing the client event, actually some client code. When autopostback= "true", the server control will automatically return without having to be thrown through the click event, and when autopostback= "false", the postback must be raised by an event similar to click.
The above is a detailed description of the implementation of the capture postback event. In general, it is not very complex, however, the specific application of the capture return event is very flexible, far from so simple, which requires the reader to continue to practice in order to understand deeply.
3. Summary
This paper first introduces the implementation method of capturing the return event for the custom server control by using ASP.net 2.0 technology. Through this content, I believe that the reader can grasp the basic method of capturing the return event of the control. In the following article, I will continue to introduce another core aspect of implementing event handling-processing of postback data.
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.
A Free Trial That Lets You Build Big!
Start building with 50+ products and up to 12 months usage for Elastic Compute Service