Processing the returned data of ASP. NET Server controls is a complicated process to implement events for custom server controls. Developers not only need to capture the return event according to the method described in the previous article, but also sometimes need to participate in the return data processing process. This article describes how to process the returned data through typical applications.
ASP. NET Server Control 1. Implement the process of returning data
During the capture and return events described in the previous article, the control data uploaded back to the server is usually not involved. Developers can implement the IPostBackEventHandler interface to successfully capture events and define Event Handlers for them. However, some server controls involve changes in the returned data during the application process. For example, a custom control is an input control. When a user inputs and returns data, some events may occur due to changes in the returned data. To solve the preceding problems, the control class must implement the IPostBackDataHandler interface. The interface declaration code is listed below.
- public interface IPostBackDataHandler{
-
- public bool LoadPostData ( string postDataKey, NameValueCollection postCollection );
-
- public void RaisePostDataChangedEvent ();
-
- }
The IPostBackDataHandler interface is used to create a server control for the form data that needs to be uploaded back to the server by the client. As shown in the code above, this interface includes two methods: LoadPostData and RaisePostDataChangedEvent.
Similar to the implementation of capture callback events, it is incomplete to implement interfaces only in the control class. The following summarizes the two key points that must be achieved in order to process the returned data.
First, you must set the property value of the Control name to UniqueID in the control rendering. This is because, after a callback occurs, the page framework searches for the UniqueID value of the server control implementing IPostBackDataHandler in the sent content, and then calls the LoadPostData method.
Second, the control class must implement the IPostBackDataHandler interface and the LoadPostData and RaisePostDataChangedEvent methods. The LoadPostData method is used to check the data submitted to the server. This method contains two parameters: postDataKey indicates the Key Value used to identify the data in the control. postData is the set of submitted data. It adopts the Key/Value structure to facilitate access using the index name. To access control data in the Set, use the following code: "string nData = postData [postDataKey];". In the LoadPostData method, compare the data value sent by the new data client with the data value previously submitted to the old data client) to determine the return value of the method. If the old and new data are the same, the data is not modified, and the return value of the method is false. If the new and old data are different, the old data has been modified by the client, and the return value is true. The following is a simple application of the LoadPostData method.
- Public Virtual BoolLoadPostData (StringPostDataKey, NameValueCollection postData)
- {
- StringPresentValue = Text;
- // Old data
- StringPostedValue = postData [postDataKey];// New data
- // Check new and old data
- If(PresentValue. Equals (postedValue) | presentValue =Null){
- Text = postedValue;
- Return True;
- }
- Return False;
- }
If the LoadPostData method returns true, the. NET Framework automatically calls the RaisePostDataChangedEvent method. This method uses signals to require the server control object to notify the ASP. NET application that the control status has changed. The control developer can define the events caused by data changes in this method. The OnTextChanged method is called as follows:
- public virtual void RaisePostDataChangedEvent()
- {
- OnTextChanged(EventArgs.Empty);
- }
The above are the key points for processing the returned data. Mastering these points is of vital significance for event processing. At the same time, the content also describes the process of processing the returned data in the following. NET Framework:
1) First, search for the UniqueID that matches the server control that implements IPostBackDataHandler in the sent content.
2) Call the LoadPostData method and return the bool value.
3) if the LoadPostData method returns true, call the RaisePostDataChangedEvent method.
4) execute the OnEvent method defined in the RaisePostDataChangedEvent method.
ASP. NET Server controls 2. Typical applications
The following describes the core process of processing the returned data through a typical instance. Create a custom Text box control WebCustomControl, whose Text attribute Text is changed because it is returned. The control triggers the TextChanged event after loading the returned data. The source code of the control class is as follows:
- UsingSystem;
- UsingSystem. Collections. Generic;
- UsingSystem. ComponentModel;
- UsingSystem. Text;
- UsingSystem. Web;
- UsingSystem. Web. UI;
- UsingSystem. Web. UI. WebControls;
- NamespaceWebControlLibrary {
- [DefaultProperty ("Text")]
- [ToolboxData ("<{0}: WebCustomControl runat = server ></ {0}: WebCustomControl>")]
- Public ClassWebCustomControl: WebControl, IPostBackDataHandler {
- // Implement the Text attribute
- [Bindable (True)]
- [Category ("Appearance")]
- [DefaultValue ("")]
- [Localizable (True)]
- Public StringText {
- Get{
- StringS = (String) ViewState ["Text"];
- Return(S =Null)? String. Empty: s );
- }
- Set{
- ViewState ["Text"] = Value;
- }
- }
- // Override the control rendering method RenderContents
- Protected Override VoidRenderContents (HtmlTextWriter output ){
- Output. addattriter( HtmlTextWriterAttribute. Type,"Text");
- Output. addattriter( HtmlTextWriterAttribute. Value, Text );
- Output. addattriter( HtmlTextWriterAttribute. Name,This. UniqueID );
- Output. RenderBeginTag (HtmlTextWriterTag. Input );
- Output. RenderEndTag ();
- }
- // Define the event object EventTextChanged
- Private Static Readonly ObjectEventTextChanged =New Object();
- # Region implement IPostBackDataHandler Member
- BoolIPostBackDataHandler. LoadPostData (StringPostDataKey, System. Collections. Specialized. NameValueCollection postCollection ){
- // Compare the initial data presentValue and the returned data postedValue
- StringPostedValue = postCollection [postDataKey];
- StringPresentValue = Text;
- If(PresentValue =Null| PostedValue! = PresentValue ){
- Text = postedValue;
- Return True;
- }
- Return False;
- }
- VoidIPostBackDataHandler. RaisePostDataChangedEvent (){
- OnTextChanged (EventArgs. Empty );
- }
- # Endregion // implement the event handler OnTextChanged
- Private VoidOnTextChanged (EventArgs eventArgs ){
- EventHandler textChangedHandler = (EventHandler) Events [EventTextChanged];
- If(TextChangedHandler! =Null){
- TextChangedHandler (This, EventArgs );
- }
- }
- // Implement the event property structure for TextChanged
- Public EventEventHandler TextChanged {
- Add {
- Events. AddHandler (EventTextChanged, value );
- }
- Remove {
- Events. RemoveHandler (EventTextChanged, value );
- }
- }
- }
- }
The above source code implements some important content.
1) The control class must implement IPostBackDataHandler, so that the control can be involved in back-to-back data processing.
2) define the property Text, whose property value is saved in ViewState. When the page is returned, the ViewState containing the Text property value will be submitted to the server.
3) override the RenderContents method and define the control rendering logic in this method.
4) Implement the IPostBackDataHandler method LoadPostData. Compare whether the data sent by the client is the same as the data submitted to the client by the previous server. If the data is the same, it indicates that the data has not been modified, false is returned. If the data is different, it indicates that the data has been modified by the client, and true is returned.
5) Implement the IPostBackDataHandler method RaisePostDataChangedEvent. If the returned value of LoadPostData is true, the OnTextChanged method must be called.
6) define the event property structure TextChanged. In the Events event Delegate list, define Add and Remove accessors for the EventTextChanged event Delegate object.
7) define the OnTextChanged method.
The Default. aspx source code of the custom Server Control is as follows:
- <%@ Page Language ="C #"AutoEventWireup ="True"CodeFile ="Default. aspx. cs"Inherits ="_ Default"%>
- <%@ Register TagPrefix ="Wcl"Assembly ="WebControlLibrary"Namespace ="WebControlLibrary"%>
- <! DOCTYPE html PUBLIC"-// W3C // dtd xhtml 1.0 Transitional // EN"
- Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <Script runat ="Server">
- VoidDemow.textchanged (ObjectSender, EventArgs e ){
- Label1.Text ="What you entered in the text box is"+ Demo1.Text;
- }
- </Script>
- <Html xmlns =Http://www.w3.org/1999/xhtml">
- <Head runat ="Server">
- <Title> process returned data </title>
- </Head>
- <Body>
- <Form id ="Form1"Runat ="Server">
- <Div>
- <Wcl: WebCustomControl ID ="Demo1"Runat ="Server"OnTextChanged ="Demow.textchanged"/>
- <Asp: Button ID ="Button1"Runat ="Server"Text ="Submit"/>
- <Br/>
- <Asp: Label ID ="Label1"Runat ="Server"Font-Size ="Small">
- </Asp: Label>
- </Div>
- </Form>
- </Body>
- </Html>
In the preceding code, a WebCustomControl control is defined, and the demo1_TextChanged event processing method is defined for this control. This method requires that the Text attribute value of the Label control be modified. 1 and 2.
Figure 1 page Initialization
Figure 2 after the page is submitted
Some readers may misunderstand that the above instance defines the event handling method for the Click Event of the submit button. Otherwise. This instance does not define a processing method for the Click event for the submit button, but is done by processing the returned data and defining the TextChanged event of the WebCustomControl control.
ASP. NET Server controls 3. Summary
This article introduces how to implement ASP. NET Server controls to process returned data. Mastering these contents will lay a good foundation for developing high-quality server controls. So far, through the introduction of the three articles, I believe that the reader has mastered the basic methods to implement events for custom server controls. In the subsequent content, I will continue to introduce other content that uses ASP. NET technology to create server controls.
- Analysis on the use of HtmlTextWriter class in ASP. NET control development skills
- Analysis on ComboBox display of ASP. NET control development skills
- Analysis of Custom Controls Based on ASP. NET control development
- Analysis on the use of the Render method for ASP. NET Server controls
- Development of ASP. NET Server controls