Implementation of event and data sending-back mechanisms

Source: Internet
Author: User
Tags button type

Client return event interface ipostbackeventhandler

This article is excerpted from the book Ding Jie Niu: vertically cutting ASP. NET 3.5 controls and component development technology

 

The control must implement the system. Web. UI. ipostbackeventhandler interface. This interface allows the control to trigger an event on the server to respond to sending back from the client. The ipostbackeventhandler Interface contains a method.
/// <Summary>
/// For more information about this book, see:
// Http://blog.csdn.net/ChengKing/archive/2008/08/18/2792440.aspx
/// </Summary>
Public interface ipostbackeventhandler
{
Void raisepostbackevent (string eventargument );
}
The eventargument parameter indicates the optional event parameters to be passed to the event handler. Generally, this parameter can be used to determine different event sources for different logic processing. At the end of this chapter, an example is provided to illustrate the eventargument parameter usage. After sending the message back, the page framework searches for the sent content and determines whether the sent name corresponds to the uniqueid of the server control implementing ipostbackeventhandler. If yes, the page Framework calls the raisepostbackevent method on the control (after a change event is triggered ).
The following code snippet shows the raisepostbackevent implementation that triggers a click event on the server:
/// <Summary>
/// For more information about this book, see:
// Http://blog.csdn.net/ChengKing/archive/2008/08/18/2792440.aspx
/// </Summary>
Public void raisepostbackevent (string eventargument)
{
Buttoneventargs E = new buttoneventargs (eventargument );
Onclick (this, e );
}
In this method, the onclick (e) event function is called, which includes two parameters: the first parameter is the current control itself (that is, the sender in the general event body, the type is generally object ); the second parameter E is a buttoneventargs parameter type object that inherits from system. the parameter class implemented by the eventargs class. In this class, any attributes related to the Code logic can be defined as parameters of the event body.
Finally, raisepostbackevent can be executed only when it is triggered by the client. The following code can cause server events:
/// <Summary>
/// For more information about this book, see:
// Http://blog.csdn.net/ChengKing/archive/2008/08/18/2792440.aspx
/// </Summary>
Protected override void render (htmltextwriter output)
{
Output. Write ("<input type = submit name =" + this. uniqueid +
"Value = 'click me'/> ");
}
This code outputs an HTML button tag and sets it to the submit type. It is very important that you do not forget to set its name attribute, because after sending back, the page framework will search for the content to be sent, determine whether the sent name corresponds to the uniqueid of the server control that implements ipostbackeventhandler. If yes, the page Framework calls the raisepostbackevent method on the control. Otherwise, if the name value of the button is not set to the uniqueid attribute value, when you click a button, the page framework will not trigger the raisepostbackevent method of the control, because only the buttons named uniqueid (Server Control Server ID) will be registered as controls with the ipostbackeventhandler interface function.
The following is a simple and complete example to understand the event sending and processing mechanism. The Code is as follows:
/// <Summary>
/// For more information about this book, see:
// Http://blog.csdn.net/ChengKing/archive/2008/08/18/2792440.aspx
/// </Summary>
[Defaultevent ("click")]
[Toolboxdata ("<{0}: postbackeventcontrol runat = Server> </{0}: postbackeventcontrol>")]
Public class postbackeventcontrol: control, ipostbackeventhandler
{
Public event eventhandler click;
Protected virtual void onclick (eventargs E)
{
If (Click! = NULL)
{
Click (this, e );
}
}
Public void raisepostbackevent (string eventargument)
{
Onclick (eventargs. Empty );
}
Protected override void render (htmltextwriter output)
{
Output. Write ("<input type = submit name =" + this. uniqueid + "value = 'click me'
/> ");
}
}
The content of this control is relatively simple. Only one button of the submit type is output. Because the button type is submit, when you click the button, it can already submit the event to the server, however, the main control cannot capture the button event. The control must have two conditions for capturing and processing this event: first, the master control inherits the ipostbackeventhandler interface and implements the raisepostbackevent method; second, it must have a client tag with the uniqueid value, the page framework only recognizes the name attribute of the control. Only when both conditions are met can the control have the opportunity to capture and process events.
The function of statement [defaultevent ("click")] is to define a click event as the default event. Generally, the most common event is defined as a default event. If a default event is defined, the system automatically displays the default event from the source code view (*. aspx) switch to the following code (*. CS) page, and can automatically register default events; otherwise, only switch to the background code, but do not register any events.
The top sentence in the class code:
Public event eventhandler click;
This statement defines a delegate event. Eventhandler is a predefined delegate dedicated to event handler methods that represent events that do not generate data. If the event generates data, you must provide the Custom Event Data Type and create a delegate (the second parameter is of the custom type ), you can either use the generic eventhandler delegate class and use a custom type to replace the generic type parameter.
In section 5.1.1, we talked about the meaning of an event and its completed functions, that is, to execute the functions of another function class in one class, to execute a Function Method in another class, the reference of another method must be retained in the current class. Delegation and events occur to implement this function. A delegate is similar to a function pointer in C ++. It generally points to a method. In this way, when the delegate in this class is called, it is equivalent to calling the method in another class.
To associate an event with the event handling method, add the delegated instance and delegated event instance to the event, for example:
This. button1.click + = new eventhandler (button#click );
Unless the delegate is removed, the event handler is called whenever this event occurs.
In addition, the delegation is widely used and has more complex applications. For example, you can define a delegate to point to multiple events at the same time, that is, a delegate to specify an event list, A series of events can be executed when a delegate is called. I will not talk about the concept of event delegation here. For more information about the concept of event delegation, please refer to the official Microsoft documentation, which has already been detailed.

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.