asp.net custom control postback data implementation scheme and code

  • 2020-05-19 04:36:27
  • OfStack

In a custom control that implements asp.net, to implement postback or post data, the custom control must implement the IPostBackDataHandler interface, in which there are two methods, one is LoadPostData and the other is RaisePostDataChangedEvent. The first method is used to populate the properties associated with the custom control when the data is sent back, and the second method is used to fire the events associated with the control. In other words, these two methods should be called when the data is submitted.

However, when we implemented our control, we inherited IPostBackDataHandler, but when we submitted the data, we found that the program did not call the method in IPostBackDataHandler. It has been found that in order to implement data postback, in addition to implementing the IPostBackDataHandler interface, you also need to register the control as a control that requires a number of postbacks. To register, override the OnPreRender method in a custom control and call Page.RegisterRequiresPostBack (this) in the method. This code. Write it as follows:


public class MyControl : WebControl, IPostBackDataHandler{ 

protected override void OnPreRender(EventArgs e) 
{ 
Page.RegisterRequiresPostBack(this); 

base.OnPreRender(e); 
} 

} 

By implementing the above code, when the data is submitted, you will find that the method of IPostBackDataHandler interface is called. Next, how to do it, you know.

Related articles: