Leverage the ASP.net 2.0 client callbacks limit postbacks

Source: Internet
Author: User
Tags current time net reference variable client
Asp.net| Client

From the outset, developers have been constrained by the feature of a web that has no fixed state. That is, once a page has been requested and loaded, the client to server side of the connection continues. Ajax actions focus on the XMLHTTP object, which makes communication between servers possible. The latest version of ASP.net (2.0) provides its own way to obtain server requests and no longer require user participation.

Unable to connect

Over the years, people have come up with a variety of ways to circumvent the fixed-state limitations of Web applications. The main approach is to reduce the number of page requests or overloads to avoid affecting user perceptions. For example, many developers use a hidden framework as a data source, so that data can be easily sent or received, and some developers also choose to load all the required add-ons at the start, thereby reducing the amount of subsequent page loads. However, the problem arises when a server-side call must be requested, leading to the introduction of AJAX group technology. Ajax uses XMLHTTP objects and XML and client script (such as JavaScript) to handle asynchronous server calls.

Asp. NET model

When the page is requested by the user, ASP. NET page starts to occur and is loaded on the requesting client. Users interact with the page through various actions, such as clicking a button. These actions may trigger a server-side call called postback (for example, the page returned to the requesting host as a result of the action is an updated version of the page that was reloaded)

There is a price to pay for the page return. For example, the client state may be lost and the user experience may be affected when communicating with the server because they are waiting for traffic and page overloading. Ajax methods solve these problems by using a single server to assist with asynchronous communication and do not affect the user experience. A similar approach can be achieved by using the ICallbackEventHandler interface of ASP.net 2.0.

Implement callback

Callback is a function that combines a particular user interface object. It performs an action as a response to an object event. An event can be either a large number of mouse clicks or any of the other events.

There are some differences in implementing callbacks and standard Web pages in asp.net 2.0. Here is a list of the areas you need to modify in your page code:

1, the page must implement ICallbackEventHandler interface.

2, the page must implement the ICallbackEventHandler interface RaiseCallbackEvent method, call this method on the server to perform the callback function.

3, the page must implement the ICallbackEventHandler interface of the GetCallbackResult method, this method will be the callback function of the results returned to the client.

With the above code modifications, you can use the callback function on the client page (HTML source code). The page must include a client function to perform the actual server request and the return result of the receiving server request.

<%@ Page language= "C #"%>
<%@ Implements interface= "System.Web.UI.ICallbackEventHandler"%>
<! DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 transitional//en" >
<title>asp.net callback</title>
<script language= "C #" runat= "Server" >
void Page_Load (object sender, EventArgs e) {
String Callbackref;
String Callbackscript;
Callbackref= Page.ClientScript.GetCallbackEventReference (This, "Arg", "GetData", "");
Callbackscript = "function CallServer (arg, context) {" + Callbackref + ";}";
Page.ClientScript.RegisterClientScriptBlock (this. GetType (), "CallServer", Callbackscript, True);
}
public string GetCallbackResult () {
Return "The current time is:" + datenow.tostring ();
}
public void RaiseCallbackEvent (String eventargument) {
}
</script>
<script type= "Text/javascript" >
function GetData (arg, context) {
Results.innertext = ';
}
</script><form id= "Frmcallback" name= "Frmcallback" runat= "Server" >
<input type= "button" value= "Callback"/>
<br><span id= "Results" ></span>
</form></body>

The C # page in the provides an instance of implementing callback. Here is a partial code description:

1, the Page_Load event in this page sets a reference to the callback function through the GetCallbackEventReference method in the ClientScript property of the page. This method can accept these parameters: a reference to the page, a parameter name to pass the data, a client function name to receive the callback data, and a parameter name for any environment variable you want to get, in this case the environment variable is not used.

2, the included reference function is used to create the function used to invoke the server (in this case, the Callbackscript variable). Of course, the name of the parameter that is accepted by the generated function must match the name passed to the GetCallbackEventReference method. Finally, the callback script must be registered through the RegisterClientScriptBlock method in the Page Object ClientScript property.

3, the GetCallbackResult method provides the input result returned by the callback function. The current data and time on the server in this example are returned.

4. The client function receiving callbacks is loaded in the header of the page (in this case, GetData). This function name must match the one passed to the GetCallbackEventReference method. The function receives two string values as the return value and an optional second value as the environment variable value returned by the server.

5, the button in the page and the callback function are associated. In this case, the HTML span object receives the callback return result.

A smooth user experience

Avoid page overloading simplifies the user experience reduces the amount of data transfer between the client and server side. You can use the Ajax method to provide this functionality. You can also use the callback function of ASP.net 2.0 to provide support. The application of these technologies is multifaceted, and in any case, any improvement in the user experience is beneficial to the business.



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.