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

Source: Internet
Author: User

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

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.