Asp.net Control Development Study Notes (6) -- Data Return

Source: Internet
Author: User
Asp.net Control Development Study Notes (6) ---- data return

In Asp.net, using the Http Post callback mechanism means that the client can store the status again and most of the controls in the .Asp.net that can be received on the server provide the function of storing its own status and trigger corresponding events when its status changes. The IPostDataHandler interface provides a method for sending back data through Http Post on the server.

 

IPostDataHandler

By implementing the IPostDataHandler interface, the server can read the client's returned data without using the Page and Request object. IPostDataHandler also provides a framework for triggering events when the user's status changes. IPostDataHandler is defined as follows:

Public interface IPostBackDataHandler

{

Public bool LoadPostData (string postDataKey,

NameValueCollection postCollection );

Public void RaisePostDataChangedEvent ();

}

 

For the LoadPostData function, if the returned value is true, the following RaisePostDataChangeEvent method is triggered. In this method, we can add events to be triggered, such:

Public virtual void RaisePostDataChangeEvent ()

{

OnTextChanged (EventArgs. Empty );

}

This method has two parameters: the second parameter first, and the second parameter returns the key-value pair. The key (key) is the unique id of the control, which is determined by the Client id, in most cases, the two IDs are equal. For example, if <input id = "xx" runat = "server"/> of the client, the unique id on the server will also be "xx ", however, if the control is placed in the gridview or masterpage, The uniqueid will be the uniqueid + separator + uniqueid of the child control of the parent control. value is the return data of the corresponding control. For example:



There are only two controls on the page, TextBox Control and Button control. The first two are the built-in ViewState and verification of asp.net. The third is the TextBox with the id "cc" and the fourth is the button with the id as button1. it can be seen that the postCollection parameter is actually the data returned by all the pages that implement the IPostBackDataHandler interface, and the first parameter postDataKey is the uniqueid of the control to which the method belongs.

Therefore, you can obtain the data returned by this control this time:

PostCollection [postDataKey]

Of course, you can also obtain the data returned by other controls this time to interact with other controls, such:

PostCollection [3]

 

The following is a Demo of IPostDataHandler.

 

DEMO1Write a TextBox Control with A status

Through this Demo, we can better understand the interaction between ViewState and PostBack Data through IPostDataHandler.

The Code is as follows:

[ToolboxData ("<{0}: textboxDemo runat = server> </{0}: textboxDemo>")]

Public class TextboxDemo: Control, IPostBackDataHandler

{

String _ text;

Public virtual string Text

{

Get

{

Object text = ViewState ["Text"];

If (text = null)

Return string. Empty;

Else

Return (string) text;

}

Set

{

ViewState ["Text"] = value;

}

}

Public bool LoadPostData (string postDataKey, NameValueCollection postCollection)

{

Text = postCollection [postDataKey];

Return false;

}

Public virtual void RaisePostDataChangedEvent ()

{

}

Override protected void Render (HtmlTextWriter writer)

{

Page. VerifyRenderingInServerForm (this );

Base. Render (writer );

// Output the <INPUT type = "text"> label

Writer. Write ("<INPUT type =" "text" "name = """);

Writer. Write (this. UniqueID );

Writer. Write ("" value = "+ this. Text +"/> ");

}

}

This is a simple TextBox implementation. By implementing the IPostBackDataHandler interface, we can save the TextBox status after each request is sent back to the server. That is, the Value of the TextBox.

Note that the Render method contains Page. verifyRenderingInServerForm (this); this line of code indicates that the control must be in the <form> label. If it is not in this label, an error is reported. this method is recommended for all controls that require data return.

The usage of the TextBoxDemo control is not described here.

 

ASP.netControl status

Versions later than asp.net 2.0. You can set the EnableViewState attribute of the control to control whether a single control saves its status. For websites with large access volumes, if you do not pay attention to closing unwanted ViewState, the user will send additional ViewState strings each time the request is submitted, which leads to performance problems.

A great feature of the control status is that the control status remains available even if the ViewState is disabled.

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.