Use icallbackeventhandle to implement Ajax-like refreshing pages

Source: Internet
Author: User
Let's take a look at this example in msdn library 2005!
------ 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">

<HTML xmlns = "http://www.w3.org/1999/xhtm l">
<Head id = "head1" runat = "server">
<Title> callback test </title>
<SCRIPT type = "text/JavaScript">
Function receiveserverdata (receivedstr, context)
{
Alert (receivedstr );
}
</SCRIPT>
</Head>
<Body>
<Form ID = "form1" runat = "server">
<Input type = "button" value = "Callback" onclick = "callserver ('argument', 'context')"/> <br/>
</Form>
</Body>
</Html>

-------- Clientcallbacp. aspx. CS ----------- // 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", "eseserverdata ","");
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 with return argument
Returnstr = eventargument. toupper ();
Return;
}

// Function that sends result?
Public String getcallbackresult ()
{
Return returnstr;
}
}

The client uses javaseserverdata to receive the data returned by the server,
<SCRIPT type = "text/JavaScript">
Function receiveserverdata (receivedstr, context)
{
Alert (receivedstr );
}
</SCRIPT>

Use callserver ('argument', 'context') to transmit data to the server.

The client is OK, followed by the server,
Implement the icallbackeventhandle interface,
Private string returnstr;
Public void raisecallbackevent (string eventargument)
{
Returnstr = eventargument. toupper ();
Return;
}

Public String getcallbackresult ()
{
Return returnstr;
}

Finally, associate the client method with the server method and implement it in page_load,
Clientscriptmanager CM = page. clientscript;
String cbreference = cm. getcallbackeventreference (this, "Arg", "eseserverdata ","");
String callbackscript = "function callserver (ARG, context) {" + cbreference + ";}";
Cm. registerclientscriptblock (this. GetType (), "callserver", callbackscript, true );

Success!

This method can also achieve a non-refreshing page like Ajax, and it is very simple to implement.

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.