ASP. NET 2.0 Server Control Processing back data

Source: Internet
Author: User

For customServerControl Implementation events are a complicated process. 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.

  1. process the returned 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, the return value of the method is determined by comparing the new data (the data value sent by the client) with the old data (the data value previously submitted to the client. 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 bool LoadPostData (string postDataKey, NameValueCollection postData)
{
String presentValue = Text;
// Old data
String postedValue = 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.

  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:

Using System;
Using System. Collections. Generic;
Using System. ComponentModel;
Using System. Text;
Using System. Web;
Using System. Web. UI;
Using System. Web. UI. WebControls;
Namespace WebControlLibrary {
[DefaultProperty ("Text")]
[ToolboxData ("<{0}: WebCustomControl runat = server> </{0}: WebCustomControl>")]
Public class WebCustomControl: WebControl, IPostBackDataHandler {
// Implement the Text attribute
[Bindable (true)]
[Category ("Appearance")]
[DefaultValue ("")]
[Localizable (true)]
Public string Text {
Get {
String s = (String) ViewState ["Text"];
Return (s = null )? String. Empty: s );
}
Set {
ViewState ["Text"] = value;
}
}
// Override the control rendering method RenderContents
Protected override void RenderContents (HtmlTextWriter output ){
Output. AddAttribute (HtmlTextWriterAttribute. Type, "text ");
Output. addattriter( HtmlTextWriterAttribute. Value, Text );
Output. AddAttribute (HtmlTextWriterAttribute. Name, this. UniqueID );
Output. RenderBeginTag (HtmlTextWriterTag. Input );
Output. RenderEndTag ();
}
// Define the event object EventTextChanged
Private static readonly object EventTextChanged = new object ();
# Region implement IPostBackDataHandler Member
Bool IPostBackDataHandler. LoadPostData (string postDataKey, System. Collections. Specialized. NameValueCollection postCollection ){
// Compare the initial data presentValue and the returned data postedValue
String postedValue = postCollection [postDataKey];
String presentValue = Text;
If (presentValue = null | postedValue! = PresentValue ){
Text = postedValue;
Return true;
}
Return false;
}
Void IPostBackDataHandler. RaisePostDataChangedEvent (){
OnTextChanged (EventArgs. Empty );
}
# Endregion // implement the event handler OnTextChanged
Private void OnTextChanged (EventArgs eventArgs ){
EventHandler textChangedHandler = (EventHandler) Events [EventTextChanged];
If (textChangedHandler! = Null ){
TextChangedHandler (this, eventArgs );
}
}
// Implement the event property structure for TextChanged
Public event EventHandler 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">
Void demow.textchanged (object sender, EventArgs e ){
Label1.Text = "what you entered in the text box" + 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 = "demo1_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 Page Submission

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.

  3. Summary

This article introduces how to process the 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 2.0 technology to create 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.