[ASP. NET Quick Start Guide] client-event framework

Source: Internet
Author: User
[Getting started with ASP. NET] cike
Event framework

I remember that in the early years in the country, communication mainly relied on a Special Profession: customer. If there are more people going out to make a living, they must bring a few safe home emails and pick up some clothes and food. Then they will need a trusted customer. Customer
You need to have a culture, know the situation of the major docks, and have a strong pair of bones and muscles, carrying heavy luggage. The customer's footsteps are the bond between villages and cities.

Yu Qiuyu "Cultural hardships and hardships • cike"


Blood cases caused by a steamed bread-
Sending back and events

In a Web-based distributed system, users usually submit forms, and browsers generate corresponding HTTP
POST request to submit an interactive application. This process is calledResend(PostBack ). In the same web page, there are often many HTML tags
It can cause sending back, and the application is submitted to the server for processing.

The control corresponds to the HTML tag of the client and has its own status and behavior. Each time a user sends a message, one or more controls on the page are called to modify the status. That is to say, the circle in the cup
Think 10, a metaphor for the controls) are associated. The user switches one of them, which may cause other circles to vibrate. Extended, when user operations or internal system status changes,
Class, you need to send a message to the associated class to adjust the status of the associated class. In the. NET Framework, this message is calledEvent(Event ),
The class for sending messages is calledEvent Source(Event
Source), the Association class is calledEvent Receiver(Event
Sink ). The process of sending back is essentially the behavior function of the event receiver called by the event source.Callback(Callback ).

We do not want to determine the callback object during compilation. Otherwise, this strong coupling relationship means that we need to put a string of correlated circles into the cup each time. Instead, we want to determine the callback relationship at runtime,
In the. NET Framework, this method is definedDelegate(Delegate ).
Think 7 and think 8
"Has a preliminary understanding of it. Events are based on the publishing-subscription mechanism. Each class that generates an event has a delegate member (publishing mechanism). during system initialization, the receiver or its
Its class needs to bind the specific event handler to the delegate member (subscription mechanism). during runtime, the system automatically completes the callback.

-Server events caused by user operations

"At last, a woman came to whisper to the client: 'Take care of him, bring things several times in the future, and never let it go. 'You told him that the things cannot be stored in Shanghai? I'm a female. How can I be a thief?
What do you do '...... The customer nodded calmly ."

The user will perform various operations on the page elements in the client browser. The browser can capture these operations and respond accordingly through scripting languages such as JavaScript, but for the server, it
But often turn a blind eye. To generate server-side events, you must enable the form elements corresponding to the event source to trigger a re-release with distinctive features during the design period, so that the page can be correctly identified and passed to the control for corresponding response.
To complete the ing process from user operations to events.

Interfaces for ASP. NET
Ipostbackeventhandler is used as a message for the client to bring back messages from afar. It contains a method:
Raisepostbackevent. After the callback, the page will find the control that matches the uniqueid that triggers the callback HTML element in the control tree, and
Call this method. The following example shows a custom control that depends on the event triggered by a user click.

// Mycontrols. CS custom control set <
Using system;
Using system. componentmodel;
Using system. Web. UI;
Using system. Web. UI. webcontrols;

Namespace essay

Public class mybutton: webcontrol, ipostbackeventhandler


// Define the control property text

Public Virtual string text




String S = (string) viewstate ["text"];

Return (S = NULL )? String. Empty: S;


Set {viewstate ["text"] = value ;}


// Generate the HTML code corresponding to the control

Protected override void render (htmltextwriter writer)

Writer. Write ("<input type = submit name =" + this. uniqueid + "value = '" + this. Text + "'/> ");


// Define the click event Delegate

Public event eventhandler click;

// Map the submitted client to a custom Click Event

Void ipostbackeventhandler. raisepostbackevent (string eventargument)

{Onclick (eventargs. Empty );}

// Implement callback

Protected virtual void onclick (eventargs E)

{If (Click! = NULL) Click (this, e );}


■ Luggage-
Server events caused by data sending back

Once, a girl from a family in the village was about to marry. Her father made a living in Shanghai, and the old man-in-law brought two red silk ."

In addition to relying on user operations to trigger events, we often need to modify the status of the corresponding control based on the user data returned to trigger events.

The returned client form data is centrally organized into
In the system. Collections. Specialized. namevaluecollection instance, the page uses the uniqueid in the control tree.
Search for matching controls. If matching controls implement interfaces
Ipostbackdatahandler is called
The loadpostdata method updates the status and returns the update identifier,
The raisepostdatachangedevent method checks the identifier to cause an event. The following example shows an example of a custom control that depends on events caused by state changes. Extension
This event mechanism can be used more flexibly. For example, when a user inputs specific data, a specific event can also be triggered here.

Using system;
Using system. Web;
Using system. Web. UI;
Using system. Collections. Specialized;

Namespace essay {

Public class mytextbox: control, ipostbackdatahandler

// Define the control property text

Public String text


Get {return (string) viewstate ["text"];}

Set {viewstate ["text"] = value ;}


// Generate the HTML code corresponding to the control

Protected override void render (htmltextwriter output)

Writer. Write ("<input type = text name =" + this. uniqueid + "value = '" + this. Text + "'/> ");


// Define the textchanged event DeleGate <
SPAN style = "color:
#008000; ">

Public event eventhandler textchanged;

// Update the text status of the control and return the updated identifier.
// The namevaluecollection parameter is the resend dataset. <

Public Virtual bool loadpostdata (string postdatakey,
Namevaluecollection values)


String presentvalue = text;

String postedvalue = values [postdatakey];

If (! Presentvalue. Equals (postedvalue ))


TEXT = postedvalue;

Return true;


Return false;


// Check the update ID to raise the Custom Event textchanged <

Public Virtual void raisepostdatachangedevent ()

{Ontextchanged (eventargs. Empty );}

// Implement callback

Protected virtual void ontextchanged (eventargs E)

{If (textchanged! = NULL) textchanged (this, e );}


■ Eyes-
Non-sending events and complete control execution Lifecycle

"As long as a visitor returns to the village, his family is always busy. Most of them are not for sending and receiving emails or things. They just look at the excitement. In the eyes of the peasants, there are envy, jealousy, and much greater contempt and ridicule. These
Looking at the city in the old age of thousands of years ."

The above two event mechanisms are directly related to sending and receiving events. Using the. NET event framework, we can trigger non-sending events anywhere in the control. For example, we can add a user-transparent application to the page.
User behavior analysis and processing controls, peat the status of other controls to trigger specific events.

At this point, we have a deep understanding of the various elements related to the control execution. Finally, through figure 11-2, we will summarize the complete control execution lifecycle.

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.