Analysis of ASP. NET Server Control Processing and return data

Source: Internet
Author: User

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.

 
 
  1. public interface IPostBackDataHandler{   
  2.  
  3. public bool LoadPostData ( string postDataKey, NameValueCollection postCollection );  
  4.  
  5.  public void RaisePostDataChangedEvent ();  
  6.  

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.

 
 
  1. Public Virtual BoolLoadPostData (StringPostDataKey, NameValueCollection postData)
  2. {
  3.  StringPresentValue = Text;
  4.  // Old data 
  5.  StringPostedValue = postData [postDataKey];// New data 
  6.  // Check new and old data 
  7.  If(PresentValue. Equals (postedValue) | presentValue =Null){
  8. Text = postedValue;
  9. Return True;
  10. }
  11.  Return False;
  12. }

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:

 
 
  1. public virtual void RaisePostDataChangedEvent()  
  2. {  
  3.  OnTextChanged(EventArgs.Empty);  
  4. }  

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:

 
 
  1. UsingSystem;
  2. UsingSystem. Collections. Generic;
  3. UsingSystem. ComponentModel;
  4. UsingSystem. Text;
  5. UsingSystem. Web;
  6. UsingSystem. Web. UI;
  7. UsingSystem. Web. UI. WebControls;
  8. NamespaceWebControlLibrary {
  9. [DefaultProperty ("Text")]
  10. [ToolboxData ("<{0}: WebCustomControl runat = server ></ {0}: WebCustomControl>")]
  11.  Public ClassWebCustomControl: WebControl, IPostBackDataHandler {
  12. // Implement the Text attribute 
  13. [Bindable (True)]
  14. [Category ("Appearance")]
  15. [DefaultValue ("")]
  16. [Localizable (True)]
  17. Public StringText {
  18.  Get{
  19. StringS = (String) ViewState ["Text"];
  20. Return(S =Null)? String. Empty: s );
  21. }
  22.  Set{
  23. ViewState ["Text"] = Value;
  24. }
  25. }
  26. // Override the control rendering method RenderContents 
  27. Protected Override VoidRenderContents (HtmlTextWriter output ){
  28. Output. addattriter( HtmlTextWriterAttribute. Type,"Text");
  29. Output. addattriter( HtmlTextWriterAttribute. Value, Text );
  30. Output. addattriter( HtmlTextWriterAttribute. Name,This. UniqueID );
  31. Output. RenderBeginTag (HtmlTextWriterTag. Input );
  32. Output. RenderEndTag ();
  33. }
  34. // Define the event object EventTextChanged 
  35. Private Static Readonly ObjectEventTextChanged =New Object(); 
  36. # Region implement IPostBackDataHandler Member 
  37. BoolIPostBackDataHandler. LoadPostData (StringPostDataKey, System. Collections. Specialized. NameValueCollection postCollection ){
  38.  // Compare the initial data presentValue and the returned data postedValue 
  39.  StringPostedValue = postCollection [postDataKey];
  40.  StringPresentValue = Text;
  41.  If(PresentValue =Null| PostedValue! = PresentValue ){
  42. Text = postedValue;
  43. Return True;
  44. }
  45.  Return False;
  46. }
  47. VoidIPostBackDataHandler. RaisePostDataChangedEvent (){
  48. OnTextChanged (EventArgs. Empty );
  49. # Endregion // implement the event handler OnTextChanged 
  50. Private VoidOnTextChanged (EventArgs eventArgs ){
  51. EventHandler textChangedHandler = (EventHandler) Events [EventTextChanged];
  52.  If(TextChangedHandler! =Null){
  53. TextChangedHandler (This, EventArgs );
  54. }
  55. }
  56. // Implement the event property structure for TextChanged 
  57. Public EventEventHandler TextChanged {
  58. Add {
  59. Events. AddHandler (EventTextChanged, value );
  60. }
  61. Remove {
  62. Events. RemoveHandler (EventTextChanged, value );
  63. }
  64. }
  65. }
  66. }

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:

 
 
  1. <%@ Page Language ="C #"AutoEventWireup ="True"CodeFile ="Default. aspx. cs"Inherits ="_ Default"%>
  2. <%@ Register TagPrefix ="Wcl"Assembly ="WebControlLibrary"Namespace ="WebControlLibrary"%>
  3. <! DOCTYPE html PUBLIC"-// W3C // dtd xhtml 1.0 Transitional // EN" 
  4.  Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  5. <Script runat ="Server">
  6.  VoidDemow.textchanged (ObjectSender, EventArgs e ){
  7. Label1.Text ="What you entered in the text box is"+ Demo1.Text;
  8. }
  9. </Script>
  10. <Html xmlns =Http://www.w3.org/1999/xhtml">
  11. <Head runat ="Server">
  12. <Title> process returned data </title>
  13. </Head>
  14. <Body>
  15. <Form id ="Form1"Runat ="Server">
  16. <Div>
  17. <Wcl: WebCustomControl ID ="Demo1"Runat ="Server"OnTextChanged ="Demow.textchanged"/>
  18. <Asp: Button ID ="Button1"Runat ="Server"Text ="Submit"/>
  19. <Br/>
  20. <Asp: Label ID ="Label1"Runat ="Server"Font-Size ="Small">
  21. </Asp: Label>
  22. </Div>
  23. </Form>
  24. </Body>
  25. </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.

  1. Analysis on the use of HtmlTextWriter class in ASP. NET control development skills
  2. Analysis on ComboBox display of ASP. NET control development skills
  3. Analysis of Custom Controls Based on ASP. NET control development
  4. Analysis on the use of the Render method for ASP. NET Server controls
  5. Development of ASP. NET Server controls

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.