ASP.net 2.0 server controls to process the postback data

Source: Internet
Author: User
Tags bool

Implementing events for custom server controls is a more complex process. Not only do developers need to capture the postback event based on the methods described in the previous article, but they also sometimes need to participate in the postback data processing process. This article will introduce a method of processing the return data through a typical application.

1. Implement processing return Data

In the process of capturing a return event, which is described in the previous article, the control data that is uploaded to the server is often not involved. Developers primarily implement the IPostBackEventHandler interface to successfully capture events and define event handlers for them. However, some server controls in the application process, involving the changes in the return data and so on. For example, a custom control is an input control that, when entered and returned by a user, may cause events due to changes in the postback data. To handle the above issues, the control class must implement the IPostBackDataHandler interface. The following is an enumeration of interface declaration codes.

public interface IPostBackDataHandler{
public bool LoadPostData ( string postDataKey, NameValueCollection postCollection );
public void RaisePostDataChangedEvent ();
}

The IPostBackDataHandler interface is used when creating a server control that requires form data to be uploaded by the client to the server. As shown in the code above, the interface includes two methods, LoadPostData and RaisePostDataChangedEvent.

Similar to the implementation capture postback event, it is not complete to implement the interface in the control class alone. The following is a summary of the two key points that must be implemented in order to achieve processing of the postback data.

First, the property value of the control's name must be set to UniqueID in the control rendering. This is because after a postback occurs, the page framework will search for a value that matches the UniqueID of the server control that implements IPostBackDataHandler, before the LoadPostData method can be invoked.

Second, the control class must implement the IPostBackDataHandler interface and implement the LoadPostData and RaisePostDataChangedEvent methods. The LoadPostData method is used to check the data submitted to the server. The method contains two parameters: Postdatakey represents the key value used to identify the data within the control, PostData is a collection of submitted data that is accessible using the Key/value structure for index names. To access the control data in the collection, simply use the following code: "String ndata = Postdata[postdatakey];" "。 In the LoadPostData method, the method return value is determined by comparing the new data (the data value sent by the client) to the old data (the data value previously submitted to the client). If the old and new data are the same, the data has not been modified, the method return value is false, and if the old data is different, it indicates that the legacy has been modified by the client and the method returns the value TRUE. The following is a simple application of the LoadPostData method.

public virtual bool LoadPostData(string postDataKey,NameValueCollection postData)
{
 string presentValue = Text;
 //旧数据
 string postedValue = postData[postDataKey];//新数据
 //检查新旧数据
 if(presentValue.Equals(postedValue) || presentValue == null) {
  Text = postedValue;
  return true;
 }
 return false;
}

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.