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.
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.
// MyControls.cs 自定义控件集
using System;
using System.ComponentModel;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace essay
{
public class myButton:WebControl,IPostBackEventHandler
{
//定义控件属性Text
public virtual string Text
{
get
{
string s =(string)ViewState["Text"];
return (s==null)?string.Empty:s;
}
set {ViewState["Text"]=value;}
}
//生成控件对应的HTML代码
protected override void Render(HtmlTextWriter writer)
{
writer.Write("<INPUT TYPE=submit name=" + this.UniqueID + " Value='"+this.Text+"' />");
}
//定义Click事件委托
public event EventHandler Click;
//把客户端提交映射到自定义的Click事件
void IPostBackEventHandler.RaisePostBackEvent(string eventArgument)
{ OnClick(EventArgs.Empty); }
//实现回调
protected virtual void OnClick(EventArgs e)
{ if(Click!=null)Click(this,e); }
}
}