Basic concepts of Asp.net: Events

Source: Internet
Author: User

. NET FrameworkEvent in

 

Event Architecture

The object that triggers the event is called the event source. Event source publishes events that can be triggered. The event type is always the delegate type. The signature of this delegate type defines the signature of the method for processing the event. The Object Defining the event handler method is called the event receiver. The event recipient subscribes to the event processing method provided by the event receiver to be processed on the event source must be consistent with the signature of the method subscribed for the event. The event source uses multicast delegation to track these event handler methods. When an important event occurs, the event source triggers an event. After the event is triggered, the event source automatically uses its golden hand delegate to call the event handler method specified on each event recipient.

 

Relationship between events and Delegation

An event is a message sent by an object to indicate that an action has occurred. An action can be triggered by user operations or other programs. The object that triggers the event is called by the event Sender: the object that captures and processes the event is called by the event receiver.

In event communication, the event Sender does not know which object or method will receive/process the events that have been sent in the past. Therefore, a man-in-the-middle is required between the event source and the event receiver. This man-in-the-middle is called a delegate.

In the effective scope of the event, the delegate provides a mechanism to indicate the method to be called when an event is triggered. When an event is triggered, the delegate is used as the pointer to the called method, and the class for modifying the event does not need to know any information about the class as the observation program.

 

Define events

Before connecting an event and its processing method, you need to define an event of interest to other objects. Use the event keyword and the delegate object indicating the method signature to define the event. All event handlers of this event must follow the method signature indicated by this delegate object.

 

Public event eventhandler click;

 

According to the Conventions, events are triggered in the oneventname method.

 

Public void onclick (eventargs E)
{
// If a handler is registered for the event, the handler is triggered.
If (Click! = NULL)
{
Click (this, e );
}
}

First check to confirm that an event exists. Otherwise, the value is null. Next, call the event to raise it like a method, pass this to the control or class, and instruct it as the event sender, use a new instance of the eventargs class as the second parameter.

 

Use event

After declaring the event and its corresponding delegate, the object that wants to respond to the event must add an event handler for the event. An event handler is a method. Its signature matches the Event-related delegate and is called when an event is triggered.

 

Object. eventname + = new delegatetype (eventhandlermethod );

 

Note: When multiple handlers are assigned to an event, the call sequence of the handler is not guaranteed. Therefore, do not expect the handler to appear in a specific order.

 

Eventhandler and eventargs

Eventhandler is an event Delegate predefined in. NET Framework. It has two parameters: object type parameters and system. eventargs type parameters. The object represents the event sender, And the eventargs object represents the event data contained in the event.

In fact, all event processing methods only have these two parameters. The only difference is that the second parameter of some other event processing methods will be system. A subclass of eventargs, used to customize some event parameters we need.

 

ASP. NETEvent Model

ASP. NET event-driven is an HTTP-based technology. In ASP. NET, event triggering and event processing are performed on the client and server respectively. After an event is triggered on the client, it is sent to the server through the HTTP protocol in post mode, while the server performs corresponding processing and feedback through the ASP. NET page architecture.

 

Cache events
ASP.. NET Framework does not provide many events to the server, because the implementation of the event-driven model mechanism is implemented on the client and server separately, and the event information must be transmitted through http protocol, therefore, frequent triggering of various events will generate a great deal of traffic pressure on the entire web site (such as onmouseover events ). However, some events are triggered frequently but must be provided (such as change events). In this case, Asp. net Framework provides a compromise, that is, when such events are triggered, they are not immediately sent to the server, but cached on the client, wait until the event information is sent back to the server. Therefore, when these cached events and newly triggered events are received on the server side, ASP. NET Framework does not interpret and process these events in a specific order.

 

Using system;
Using system. Data;
Using system. configuration;
Using system. collections;
Using system. Web;
Using system. Web. Security;
Using system. Web. UI;
Using system. Web. UI. webcontrols;
Using system. Web. UI. webcontrols. webparts;
Using system. Web. UI. htmlcontrols;

/// <Summary>
/// Example of event caching
/// Author: wenye
/// Contact: stwyhm.cnblogs.com
/// </Summary>
Public partial class cacheevent: system. Web. UI. Page
{
Protected void page_load (Object sender, eventargs E)
{

}

/// <Summary>
/// Note: The textchanged event of the text box does not automatically trigger the page PostBack
/// </Summary>
/// <Param name = "sender"> </param>
/// <Param name = "E"> </param>
Protected void textbox1_textchanged (Object sender, eventargs E)
{
Response. Write ("triggers the textchanged event of the text box. <Br/> ");
}

/// <Summary>
/// Description: The selectedindexchanged event in the drop-down box does not automatically trigger the page PostBack
/// </Summary>
/// <Param name = "sender"> </param>
/// <Param name = "E"> </param>
Protected void dropdownlistincluselectedindexchanged (Object sender, eventargs E)
{
Response. Write ("raises the selectedindexchanged event in the drop-down box. <Br/> ");
}

/// <Summary>
/// Note: The Click Event of the button triggers the page PostBack
/// </Summary>
/// <Param name = "sender"> </param>
/// <Param name = "E"> </param>
Protected void button#click (Object sender, eventargs E)
{
Response. Write ("triggers the button click event. <Br/> ");
}
}

 
As shown in the preceding example, when the button event triggers PostBack, the other two events cached at the same time also run.

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.