Client callback in ASP.net 2.0

Source: Internet
Author: User
Tags implement reference
Asp.net| clients Sometimes we need to use JavaScript to callback the server to avoid page refreshes in the postback process, which not only reduces the delay in page refreshes but also eliminates the need for the server to process a large number of view state information for each postback. The overall performance of the application can be greatly improved. In ASP.net 2.0, a feature called "Client Callback" was introduced, and this built-in solution allows us to easily interact with client-side and server-end code to avoid frequent refreshes of the page due to postbacks.

In order to implement the client callback, your page class must implement a ICallbackEventHandler interface. Its statement reads as follows:

Using System;

Namespace System.Web.UI
{
Summary:
The target used to indicate that the control can act as a callback event for the server.
public interface ICallbackEventHandler
{
Summary:
Returns the result of a callback event that targets a control.
//
return Result:
The result of the callback.
String GetCallbackResult ();
//
Summary:
Handles a callback event that targets a control.
//
Parameters:
Eventargument:
A string representing the event arguments to be passed to the event handler.
void RaiseCallbackEvent (String eventargument);
}
}

The client's JavaScript is to callback the page through the Clientscript.getcallbackeventreference function, as described in the following function:

public string GetCallbackEventReference (Control control,string argument,string clientcallback,string context)

Parameters:

Parameters Role
Control Server control that handles client callbacks. The control must implement the ICallbackEventHandler interface and provide a raisecallbackevent method.
Argument Passes a parameter to the server-side RaiseCallbackEvent method from the client script.
Clientcallback The name of a client event handler that receives the results returned by the server-side event.
Context Client script information at the client before the callback is started. The result of the script is passed back to the client event handler.
return value The name of the client function that invoked the client callback.


The following is an overloaded list of clientscriptmanager.getcallbackeventreference methods

Name Description
Clientscriptmanager.getcallbackeventreference (Control, String, String, String) Gets a reference to a client function that, when invoked, initiates a client callback to the server-side event. The client function for this overloaded method contains the specified control, parameters, client script, and context.
Clientscriptmanager.getcallbackeventreference (Control, String, String, String, Boolean) Gets a reference to a client function that, when invoked, initiates a client callback to the server-side event. The client function for this overloaded method contains the specified controls, parameters, client script, context, and Boolean values.
Clientscriptmanager.getcallbackeventreference (Control, String, String, String, String, Boolean) Gets a reference to a client function that, when invoked, initiates a client callback to the server-side event. The client function for this overloaded method contains the specified control, parameter, client script, context, error handler, and Boolean value.
Clientscriptmanager.getcallbackeventreference (String, String, String, String, String, Boolean) Gets a reference to a client function that, when invoked, initiates a client callback to the server-side event. The client function for this overloaded method contains the specified target, parameter, client script, context, error handler, and Boolean value. We make a systematic description of the entire program, and list the front page code and background logic code, which allows you to have an intuitive understanding of the program.

The following is a simple example of the source code:

Default.aspx.cs:

Using System;
Using System.Data;
Using System.Configuration;
Using System.Web;
Using System.Web.Security;
Using System.Web.UI;
Using System.Web.UI.WebControls;
Using System.Web.UI.WebControls.WebParts;
Using System.Web.UI.HtmlControls;

Public partial class _default:system.web.ui.page, System.Web.UI.ICallbackEventHandler
{
Defines a string, and the result information for the callback is saved in the string
private string result;

Raise callback Event handling
public void RaiseCallbackEvent (String eventargument)
{
result = "The content returned from the server side:" + eventargument;
}

Postback callback Result
public string GetCallbackResult ()
{
return result;
}
}

Default.aspx:

<%@ Page language= "C #" autoeventwireup= "true" codefile= "Default.aspx.cs" inherits= "_default"%>

<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">

<title> Client Callback </title>

<script type= "Text/javascript" >


function CallServer (Inputcontrol, context)
{
The callback has not yet processed its preloaded display value at full time
context.innerhtml = "Load ...";
For the information you enter in the text box, and Arg is here to pass its value to the
RaiseCallbackEvent (String eventargument) method corresponds to the eventargument
arg = Inputcontrol.value;
Gets a reference to a client function that, when invoked, initiates a client callback to the server-side event.
<%= Clientscript.getcallbackeventreference (This, "Arg", "receiveserverdata", "context")%>;
}

function Receiveserverdata (result, context)
{
context.innerhtml = result;
}

</script>
<body>
<form id= "Form1" runat= "Server" >
<div>
Please enter the information: <asp:textbox id= "Txtenter" runat= "Server" ></asp:TextBox>
<input id= "Button1" type= "button" value= "Callback service Side" onclick= "CallServer (txtenter,lblshow)"/>
<br/>
<asp:label id= "lblshow" runat= "Server" width= "219px" ></asp:Label>
</div>
</form>
</body>


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.