Using Icallbackeventhandle to implement an AJAX-like non-refresh page

Source: Internet
Author: User
Tags implement return string client
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



Related Article

Cloud Intelligence Leading the Digital Future

Alibaba Cloud ACtivate Online Conference, Nov. 20th & 21st, 2019 (UTC+08)

Register Now >

Starter Package

SSD Cloud server and data transfer for only $2.50 a month

Get Started >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.