Capture and return events of ASP. net2.0 server controls

Source: Internet
Author: User
The previous article introduced the basic concepts of implementing custom server control events. This article describes how to capture a return event through a typical example.

  1. Capture the return event

If the server control needs to capture a return event from the client and want to customize the server-side event processing logic for the return 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 above Code, the ipostbackeventhandler interface only includes a member method raisepostbackevent. This method enables the Server Control to process events triggered when a form is sent to the server. The eventargument parameter indicates the optional event parameters to be passed to the event handler. Developers can use the raisepostbackevent method to implement the logic executed during the Server Control return process. Generally, the raisepostbackevent method will trigger one or more server events. The following code snippet shows the raisepostbackevent implementation that triggers the click event on the server.

Public void raisepostbackevent (string eventargument)
{
Onclick (eventargs. Empty );
}

The implementation of capturing the return event is not just to enable the server control class to implement the ipostbackeventhandler interface, but also to implement the member methods of this interface. Developers also need to pay attention to implementing other content. The following lists three key points in the process of capturing a callback event.

First, and most importantly, the custom server control class must implement the ipostbackeventhandler interface and implement the raisepostbackevent method as the interface member. This process has been introduced in the previous article.

Second, assign a uniqueid to the control.

Defining the property value of the name attribute of the control that causes the callback event is uniqueid, which is one of the keys to correctly implement the raisepostbackevent method. When a return request is triggered, the page framework searches for the sent content and determines whether the name of the sent object corresponds to the uniqueid of the server control implementing ipostbackeventhandler. If yes, the page Framework calls the raisepostbackevent method on the control. The focus here is that developers need to assign a uniqueid to the name attribute of the control in the rendering logic. The following is a simple code example.

Protected override void render (htmltextwriter output)
{
Output. Write ("<input type = submit name =" + this. uniqueid + "value = 'clickme'/> ");
}

As shown in the code above, a button is displayed in the render method render of the control, and its name attribute value is uniqueid. Only a uniqueid is assigned to the name attribute of the control that causes the return can the return event be correctly captured.

Third, implement the event property structure.

The event attribute structure is an optimized event implementation method. Before introduction, Let's first look at the common control event implementation methods. The Code is as follows.

......
Public class webcustomcontrol: webcontrol, ipostbackeventhandler {
// Declare the click event Delegate
Public event eventhandler click;
// Implement the raisepostbackevent Method
Void ipostbackeventhandler. raisepostbackevent (string eventargument ){
Onclick (eventargs. Empty );
}
// Define the onclick event handler
Protected virtual void onclick (eventargs e ){
If (Click! = NULL) {Click (this, e );}
}
......
}

The above code includes three key elements related to the event definition: 1. Define the click event Delegate; 2. the control class implements the ipostbackeventhandler interface, the onclick event handler is defined during the implementation of the interface member method raisepostbackevent. 3. The onclick event handler is implemented. The above implementation method is simple and easy to use, but it has a disadvantage, that is, the execution efficiency is low. Especially when multiple events are triggered in a class, the overhead is increased, a large amount of server resources are wasted, and the running efficiency is reduced.

To solve the above problems, we will introduce an optimized event implementation method-the event attribute structure. This structure uses the system. componentmodel. eventhandlerlist class, which provides a simple delegate list. By using the related methods provided by this class, developers can flexibly operate the event handler delegate list of controls. For example, for the click event in the control, the event attribute structure is as follows:

Protected static readonly object eventclick = new object ();
Public event eventhandler click {
Add {
Events. addhandler (eventclick, value );
}
Remove {
Events. removehandler (eventclick, value );
}
}

Before defining the event property structure, you must first define the click event Delegate object. Since each event is created only once, it must be declared as static and read-only. Then, use the addhandler and removehandler methods in the attribute structure to operate the event handler delegate list. When the page calls the click event, it adds or deletes a handler to or from the eventhandlerlist set of the control. As this implementation method is more efficient than the Common Implementation Method in the declaration process of multiple events, it is very recommended.

In addition, when an event attribute is used in the implementation of The onclick method, the delegate must be retrieved from the eventhandlerlist and converted to the eventhandler type.

Protected virtual void onclick (eventargs e ){
Eventhandler clickhandler = (eventhandler) events [eventclick];
If (clickhandler! = NULL ){
Clickhandler (this, e );
}
}

Note: The Event property structure is not applicable to the VB. NET language and can only be used in languages such as C.

 

2. Typical applications

Practically speaking, the above theoretical introduction to capturing callback events is hard to understand for readers who have never implemented server control events. This section uses a typical example to describe how to capture a callback event.

In this example, a custom Server Control webcustomcontrol is implemented. Although the control is displayed as a button, it is not inherited from the button class. When you click this button, the control will cause a return. The server automatically captures the return Click Event, raises a click event, and executes the corresponding event processing program. The following is the source code 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 ();
// Implement Click Event attributes
[Description ("Click Event property"), category ("action")]
Public event eventhandler click {
Add {
Events. addhandler (eventclick, value );
}
Remove {
Events. removehandler (eventclick, value );
}
}
 
// Override the control rendering method rendercontents

Protected override void rendercontents (htmltextwriter output ){
Output. Write ("<input type = 'submit 'name =" + this. uniqueid + "value = Please click/> ");
}
// Implement the event Method
Protected virtual void onclick (eventargs e ){
Eventhandler clickhandler = (eventhandler) events [eventclick];
If (clickhandler! = NULL ){
Clickhandler (this, e );
}
}
// Implement ipostbackeventhandler interface member
Void ipostbackeventhandler. raisepostbackevent (string eventargument ){
Onclick (eventargs. Empty );
}
}
}

In the webcustomcontrol class, the following key content is implemented for capturing and processing the return event:

· The control class webcustomcontrol implements ipostbackeventhandler;

· Set uniqueid for the name attribute value of the control that triggers callback;

· Implement the event property structure and maintain the event handler delegate list;

· Call The onclick method in the raisepostbackevent method;

The following code is the default. aspx source code of the webcustomcontrol button of the application. The result 1 and figure 2 are displayed.

<% @ Page Language = "C #" autoeventwireup = "true" codefile = "default. aspx. cs" inherits = "_ default" %>
<% @ Register tagprefix = "cc" namespace = "webcontrollibrary" assembly = "webcontrollibrary" %>
<! Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<SCRIPT runat = "server">
Void wcc1_click (Object sender, eventargs e ){
Message. Text = "you just clicked the button above ";
}
</SCRIPT>
<HTML xmlns = "http://www.w3.org/1999/xhtml">
<Head runat = "server">
<Title> capture a return event </title>
</Head>
<Body>
<Form ID = "form1" runat = "server">
<Center>
<CC: webcustomcontrol id = "wpc3" runat = "server" onclick = "wcc1_click"/> <br/>
<Asp: Label id = "message" runat = "server"> </ASP: Label>
</Center>
</Form>
</Body>
</Html>

1 and 2 are applications.


Figure 1 page initialization Figure 2

In addition, there is a property autopostback that is closely related to capturing the return event. This attribute is used for automatic return settings of controls. Many standard server controls contain this attribute. For developers, it is important to define the property in the self-created server control. You need to carefully consider the trade-off based on the functional requirements of the control. The key code implementation for this attribute is as follows:

// Define the property 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 ));
}
......
}

The code above shows that the implementation of the autopostback attribute focuses on the application of the page. getpostbackeventreference method. This method gets reference to the client-End Script Function. Calling this function will send the server back to this page and return a string representing the client event, which is actually some client code. When autopostback = "true", the server control will automatically return the message without triggering the click event. When autopostback = "false", the callback must be triggered by a similar click event.

The above describes how to capture a callback event. In general, it is not very complicated. However, the specific application for capturing callback events is very flexible, and far from that simple. This requires the reader to continue to practice in order to gain a deeper understanding.

  3. Summary

This article first introduces how to capture callback events for custom server controls using ASP. NET 2.0 technology. Through this content, we believe that the reader can basically master how to implement the control to capture the return event. In subsequent articles, the author will continue to introduce another core content for event processing-processing the returned data.

 

Related Article

Contact Us

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

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.