Novice must see ASP. NET Programming Introduction Random Guest

Source: Internet
Author: User
Tags empty execution html tags http post modify client
Remember the early years in the country, the external communications mainly rely on a special professional person: the customer. A lot of people go out to make a living, not to take a few letters of peace home, a little clothing food, it is useful to customers. The customer should have a little culture, know the situation of the major terminals, but also a strong muscles, back to the heavy baggage. The heavy footsteps of the faithful are the ties between the country and the city.
--Yu Qiuyu "Culture Bitter Brigade • the faithful"

   a bun-triggered murder-postback and events

In a web-based distributed system, the user often submits the form, the browser produces the corresponding HTTP POST request to complete the interaction process, this process is called the postback (postback). In the same Web page, there is often a lot of HTML tags that can cause postbacks, and applications are processed by the server.



Postback

Control corresponds to the client's HTML tag and has its own state and behavior. A user action causes each postback to invoke one or more control behaviors in the page to modify its state, that is, the powder Circle in the cup (the analogy of the control in the "Random Ten") is connected, the user moves one of them, may cause other powder round vibration. Expand, when user action or system internal trigger state change, the class needs to send a message to the association class, let the association class make corresponding state adjustment. In the. NET Framework, this message is called an event, and the class that sends the message is called the event source, and the association class is called the event sink. The process of postback is essentially the behavior function of the event source invocation event receiver, called a callback (callback).

We do not want to determine the object of the callback at compile time, otherwise this strong coupling means that each time you use it, you need to carry a string of associated powder to the cup. Instead, we want to determine the callback relationship at runtime, which is defined as a delegate (delegate) in the. NET Framework, and we have a preliminary understanding of it in the "Random Seven" and "Random Eight". Events are based on the publish-subscribe mechanism, each class that generates an event has a delegate member (the publishing mechanism), and when the system initializes, the receiver or other class needs to bind the specific event handler to the delegate member (the subscription mechanism), and the system automatically completes the callback at runtime.

   Message-server-side events raised by user actions

Finally, a woman came to give a message to the customer: ' Take care of him, bring things back a few times, and don't Grisse '; ' You tell him that the goods can't be stored in Shanghai? I have a woman's house, come to the robber to the thief how to do ' ... The faithful nodded in a steady place. "

Users will do various things with the page elements in the client browser, and the browser can capture these operations and respond to them by scripting languages such as javasript, but it often ignores the server. To produce a server-side event, the form element of the event source must be raised in a design period with a distinct feature postback, so that the page can be properly identified and passed to the control for a corresponding callback to complete the mapping of the user action to the event.

Asp. NET with the interface IPostBackEventHandler as a message, take back to the distant message, it contains a method: RaisePostBackEvent. After the postback, the page looks in the control tree for a control that matches the UniqueID that raised the return HTML element, and calls the method, which is dependent on the user to click on the custom control instance that raised the event.

Return (s==null)? string. Empty:s;
}
Set {viewstate["Text"]=value;}
}
//generate the corresponding HTML code for the control
protected override void Render (HtmlTextWriter writer)
{
Writer. Write ("<input type=submit name=" + this. UniqueID + "Value= '" +this. text+ "'/>");  
}
//Define Click event Delegate
public event EventHandler click;
Maps client submissions to a custom click event
void Ipostbackeventhandler.raisepostbackevent (String eventargument)
{OnClick ( Eventargs.empty);
//Implementation callback
protected virtual void OnClick (EventArgs e)
{if (click!=null) Click (this,e);}
}
}

 Baggage-server-side events raised by postback data

"Once, the village of a family of girls to get married, the girl's father in Shanghai for a living, the old letter to bring two red silk." "

In addition to relying on user actions to raise events, we often need to modify the state of the corresponding control based on the postback's user data to raise the event.

The postback's client form data is centrally collated to include the data name/ In a System.Collections.Specialized.NameValueCollection instance of a value set, the page uses UniqueID to look for a matching control in the control tree, and if the matching control implements an interface IPostBackDataHandler, the call to Loa The Dpostdata method updates the status and returns an update identity, RaisePostDataChangedEvent method checks the identity to raise the event. The following example is a custom control instance that is dependent on state changes to raise events. To expand, you can use this event mechanism more flexibly, such as when a user enters specific data, and can also raise a specific event here.

Using System;
Using System.Web;
Using System.Web.UI;
Using System.Collections.Specialized;

Namespace Essay {
public class Mytextbox:control, IPostBackDataHandler
{
Define control Property Text
Public String Text
{
get {return (String) viewstate["Text"];
set {viewstate["Text"] = value;}
}
Generate HTML code for the control
protected override void Render (HtmlTextWriter output)
{
Writer. Write ("<input type=text name=" + this. UniqueID + "Value= '" +this. text+ "'/>");
}
Defining TextChanged Event delegates
public event EventHandler TextChanged;
Updates the control's text state and returns an update identity
Parameter NameValueCollection is the postback data set
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;
}
Checking for update identities raises custom events TextChanged
public virtual void RaisePostDataChangedEvent ()
{ontextchanged (eventargs.empty);}
Implement callback
protected virtual void ontextchanged (EventArgs e)
{if (textchanged!= null) textchanged (this,e);}
}
}

   Eyes-Non postback events and complete control execution Lifecycle

"As long as the faithful return to the village, his family is always crowded." Most are not to accept letters, things, just to see a lively. In the eyes of the peasant, there is envy, jealousy, and much more, there is contempt, there is ridicule. These eyes are the city of the thousand-year-old inquiry. "

All the above two events are directly related to the postback and use. NET Event framework, we can raise a non postback event anywhere in the control. For example, we can add a user-transparent user behavior profiling control to a page and peek into other control states to raise its specific event.

At this point, we've got a thorough understanding of the various element details related to the execution of the control, and finally, in Figure 11-2, let's summarize the complete lifecycle of the control execution.



Control's full 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.