See this example on MSDN Library 2005 first!
------clientcallback.aspx------
<%@ Page language= "C #" autoeventwireup= "true" codefile= "ClientCallback.aspx.cs" inherits= "Clientcallback"%>
<! DOCTYPE HTML PUBLIC "-//w3c//dtd XHTML 1.1//en" "Http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd" >
<title> Callback test</title>
<script type= "Text/javascript"
function Receiveserverdata (RECEIVEDSTR, Context)
{
alert (RECEIVEDSTR);
}
</script>
<body>
<form id= "Form1" runat= "Server"
<input type= "Button" value= "Callback" onclick= "callserver (' argument ', ' context ')"/><br/>
</form>
</ Body>
//ClientCallback.aspx.cs
Using System;
using System.Data;
using System.Configuration;
using System.Collections;
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 ClientCallback:System.Web.UI.Page, System.Web.UI.ICallbackEventHandle R
{
void Page_Load (object sender, EventArgs e)
{
ClientScriptManager cm = Page.clientscript;
String cbreference = cm. GetCallbackEventReference (This, "Arg", "Receiveserverdata", "");
String Callbackscript = "function CallServer (arg, context) {" + Cbreference + ";}";
Cm. RegisterClientScriptBlock (this. GetType (), "CallServer", Callbackscript, True);
}
private string Returnstr;
function called by client, executed on server
public void RaiseCallbackEvent (String eventargument)
{
Do something and return argument
Returnstr = Eventargument.toupper ();
Return
}
function that sends result?
public string GetCallbackResult ()
{
return returnstr;
}
}
The client uses Receiveserverdata to receive the data returned by the server,
<script type= "Text/javascript" >
function Receiveserverdata (receivedstr, context)
{
alert (RECEIVEDSTR);
}
</script>
Use CallServer (' argument ', ' context ') to pass data to the server.
The client is OK, then the server side,
Implement Icallbackeventhandle interface,
private string Returnstr;
public void RaiseCallbackEvent (String eventargument)
{
Returnstr = Eventargument.toupper ();
Return
}
public string GetCallbackResult ()
{
return returnstr;
}
Finally, the client method is associated with the server-side approach, implemented in Page_Load,
ClientScriptManager cm = Page.clientscript;
String cbreference = cm. GetCallbackEventReference (This, "Arg", "Receiveserverdata", "");
String Callbackscript = "function CallServer (arg, context) {" + Cbreference + ";}";
Cm. RegisterClientScriptBlock (this. GetType (), "CallServer", Callbackscript, True);
Done!
This approach also achieves an AJAX-like refresh-free page and is simple to implement.
Http://pyw0818.cnblogs.com/archive/2006/05/30/413416.html