Asp. NET component Design step by step (6)

Source: Internet
Author: User
Tags http post implement
asp.net| Design asp.net page if it is through a POST request to the server, the framework will follow the event cycle generation, invoke the control, and the control (if it supports the postback) will load the postback data and map the server-side event of the control, as if it were replaying the customer's behavior (the customer presses a button , it raises the click event of the server-side control. What are the mechanisms?

If a control needs to handle a postback event, a specific interface IPostBackEventHandler interface must be implemented:

public interface ipostbackeventhandler{

void RaisePostBackEvent (String eventargument);

}

and a different interface:

IPostBackDataHandler

{

BOOL LoadPostData (String postdatakey, NameValueCollection postcollection);

void RaisePostDataChangedEvent ();

}

Once the control implements these interfaces, the page frame automatically calls this interface IPostBackDataHandler of the control after the postback data is complete. LoadPostData, allowing the control to read the post data. Postdatakey is the name of the named key in the postback data, and Namevaluecollection[postdatakey] Gets the value that the page frame passes to the control. Control should read this value, make its own internal state updates, and reflect state changes. If the control returns TRUE, indicating a server control state change, the page frame immediately calls the RaisePostDataChangedEvent method of the control. At this point, the control should define its own events that raise the external supply of those server controls. These events are often the code stage that control programmers carefully design to leave to the ASPX programmer who uses the control key.

As for the other interface IPostBackEventHandler, it is also the interface that the server invokes. We know that each control has a UniqueID, when the client triggers a client event that can cause a postback (such as pressing the Submit button), then the natural HTTP POST data to the server side, the Server page framework to the postback processing time, Retrieves whether the control supports the IPostBackEventHandler interface, and the UniqueID discovery support calls this interface's RaisePostBackEvent method immediately, indicating that a UniqueID control has an event that needs to be caught. It is clear that not all events on the client can be projected to the server side, but can be events and controls that can raise post back (that is, events that can submit form data to the server). Note that there are strict requirements UniqueID must be consistent on the server side and the client, otherwise the event cannot be mapped.

It is worthwhile for the control writer to note that if you want to implement an interface, you need to implement the interface as follows, rather than just the usual interface method name:

void Ipostbackeventhandler.raisepostbackevent (String eventargument)

{

......

}

That is, the interface is implemented for the page frame and is called by the page framework.

On the other hand, on the client side, there are actually only 2 HTML elements <inoput type=submit > and <input type=image that can cause a postback, but other client-side events can also cause the data to be returned to the server end through client-side scripting. The page class provides a series of ways to help implement other ways to trigger a postback:



public string GetPostBackClientEvent (

Control,

String argument

);

Gets a reference to a client-side script function that causes the server to send back to the form when invoked.



public string Getpostbackclienthyperlink (

Control,

String argument

To append javascript: To the beginning of the return from the GetPostBackEventReference call, so that a hyperlink postback can be processed on the server.



public string getpostbackeventreference (control);

public string GetPostBackEventReference (control, string);

Gets a reference to a client-side script function that calls the function to cause the server



If a control determines that the client raises a postback event by using the above method, then the control reference page causes the final output to include the script in the client's HTML, and the following implied variables are hidden in the script:

<input type= "hidden" name= "__eventtarget" "value=" ">

<input type= "hidden" name= "__eventargument" "value=" ">

<script language= "JavaScript" >

<!-

function __doPostBack (eventtarget,eventargument)

{

var theform=document._ct10;

Theform.__eventtarget.value= Eventtarget;

Theform.__eventargument.value= eventargument;

}

-->

</script>

Any client script can be uploaded to the server side as long as the __doPostBack method is legitimately invoked, and the server side is aware of the event that triggered the UniqueID control.



Author blog:http://blog.csdn.net/shanhe/


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.