ajax| Refresh | no Refresh | page
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"/><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