Atypical ajax practices

Source: Internet
Author: User

Preface
Ajax can be said to be the most popular word on the internet today. However, it is basically a client that calls the server asynchronously and finally returns what is needed. Currently, asp. netajax, ajax.net, or pure xmlhttp asynchronous call. In fact, asp. net2.0 also provides an asynchronous call method, that is, the Callback mechanism (compared with postback ).

ICallbackEventHandler Interface
The Callback mechanism is implemented using the ICallbackEventHandler interface, which defines two methods.

// Return the server execution result to the client
Public string GetCallbackResult ()
{
// Throw new Exception ("The method or operation is not implemented."); return rValue;
}

// Processing method when callback is triggered
Public void RaiseCallbackEvent (string eventArgument)
{
// Throw new Exception ("The method or operation is not implemented .");
}


ClientScript object
Then, the client callback Server reference and client script registration are implemented using a series of methods of the ClientScript object, that is, events on the server are triggered using the client method. After a series of events, the results are returned to the client.

// Obtain the reference of the function that the client wants to trigger the server. In this way, the client function can asynchronously trigger events on the server and perform Response Processing. Therefore, to use Callback, this is one of the required methods
GetCallbackEventReference (Control control, string argument, string clientCallback, string context );
// Register (ADD) a script (js) to the client. This is also a frequently used method.
RegisterClientScriptBlock (Type type, string key, string script );

Process instance analysis
There are so many things to be used, and the entire asynchronous call process is:
1. Put a js function (used to trigger server events) in aspx so that it can process the data returned by the server. This step can also be performed in aspx. cs generates script blocks and registers them to aspx;
2. In aspx. cs, use GetCallbackEventReference to generate a reference to the function in step 1. This step enables it to trigger server events and specify parameters and context;
3. Put a function that triggers the function in step 1 in aspx. cs (used to pass some parameters and context );
4. Trigger the function in step 3 at an appropriate position in aspx and upload the response parameters and context.

This is the whole process. I am tired and give an example:
The aspx file is as follows:

<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<Html xmlns = "http://www.w3.org/1999/xhtml">
<Head runat = "server">
<Title> Client PostBack Demo </title> <link href = "Site.css" rel = "stylesheet" type = "text/css"/>
</Head>
<Body>
<Form id = "form1" runat = "server">
<Div> Enter Category ID: <input type = "text" id = "txtCategoryID" name = "txtCategoryID"/> <input type = "button" value = "Submit" onclick = "GetProductsByCategoryID () "/> <br/>
<Br/>
<Br/>
<Div id = "MyDiv" runat = "server">
</Div>
<Asp: GridView ID = "myGridView" CssClass = "GridViewBackGround" runat = "server"/>
</Div>
</Form>
</Body>
</Html>
<Script language = "javascript" type = "text/javascript">
Function GetProductsByCategoryID ()
{
// TxtCategoryID is the textbox
CategoryID = document. getElementById ("txtCategoryID"). value;

CallServer (categoryID ,'');
}
Function compute eserverdata (rValue)
{
Document. getElementById ("myDiv"). innerHTML = rValue;
}
</Script>

The click event of the button triggers the GetProductsByCategoryID () function. GetProductsByCategoryID () calls "CallServer (categoryID,'') "(function in step 3 ), two parameters are input. The following CallServer calls the reference [GetCallbackEventReference (this, "arg", "eseserverdata", "context") of the client function (named ReceiveServerData) that will trigger the server event to return].

The aspx. cs file is as follows:

Protected void Page_Load (object sender, EventArgs e)
{
String sbReference = ClientScript. GetCallbackEventReference (this, "arg", "eseserverdata", "context ");
String cbScript = String. Empty;

// Check if the script is already registered or not
If (! ClientScript. IsClientScriptBlockRegistered ("CallServer "))
{
CbScript = @ "function CallServer (arg, context) {" + sbReference + "}";
ClientScript. RegisterClientScriptBlock (this. GetType (), "CallServer", cbScript, true );
}
}

In this case, an event is triggered. The response method is RaiseCallbackEvent (string eventArgument). The string parameter is GetCallbackEventReference (this, "arg", "ReceiveServerData", "context ").
After the preceding method is executed, another event will be triggered. The response method is string GetCallbackResult (), which returns a string to the client function that triggers the server event ReceiveServerData (), the client to be updated will respond to the change.

Conclusion
Asynchronous Server call technology is encapsulated a lot, and Microsoft is no exception, except asp. in addition to netajax, the Callback mechanism is also built in asp.net to Implement Asynchronous calls. In fact, the underlying layer is still xmlhttp encapsulation, but asp. the functions and controls of netajax are much more powerful than those of callback.

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.