Client:
CopyCode The Code is as follows: <% @ page Language = "C #" autoeventwireup = "true" codefile = "ASP. neta_jax.aspx.cs" inherits = "_ default" %>
<! Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<HTML xmlns = "http://www.w3.org/1999/xhtml">
<Head runat = "server">
<Title> </title>
<SCRIPT type = "text/JScript">
Function callserver (){
// JSON sending object
Serversum ("{Name: 'linyijia ', age: '21 '}");
}
Function getregister (RG, contex ){
Document. getelementbyid ("txtregister"). value = RG;
}
</SCRIPT>
</Head>
<Body>
<Form ID = "form1" runat = "server">
<Div>
<Br/>
Username: <input id = "txtnum1" type = "text"/>
<Br/>
Server: <input id = "txtregister" type = "text"/> <br/>
<Button id = "sumbtn" type = "button" onclick = "callserver ()"> log on </button>
</Div>
</Form>
</Body>
</Html>
Server: Copy code The Code is as follows: using system;
Using system. Collections. Generic;
Using system. LINQ;
Using system. Web;
Using system. Web. UI;
Using system. Web. UI. webcontrols;
Using system. Web. Script. serialization;
Public partial class _ default: system. Web. UI. Page, icallbackeventhandler
{
Users u = NULL;
Protected void page_load (Object sender, eventargs E)
{
// Call back the getregister Method
String callbackfun = page. clientscript. getcallbackeventreference (this, "Arg", "getregister", "context ");
// Create the serversum method. When the client calls the method, it calls back the getregister method, passes the parameter to raisecallbackevent (string eventargument), and finally passes
// The getcallbackresult () method sends the returned value to the client.
String registerfun = string. Format ("function serversum (ARG, context) {{{ 0 }}}", callbackfun );
Page. clientscript. registerclientscriptblock (this. GetType (), "serversum", registerfun, true );
}
String mssage = string. empty;
# Region icallbackeventhandler Member
Public String getcallbackresult ()
{
Return "server: Hello, your username is:" + U. Name + "your age is" + U. Age;
}
Public void raisecallbackevent (string eventargument)
{
Javascriptserializer JS = new javascriptserializer ();
U = Js. deserialize <users> (eventargument );
}
# Endregion
}
Users classCopy codeThe Code is as follows: using system;
Using system. Collections. Generic;
Using system. LINQ;
Using system. Web;
/// <Summary>
/// User Summary
/// </Summary>
Public class users
{
String name;
Public string name
{
Get {return name ;}
Set {name = value ;}
}
String age;
Public String age
{
Get {return age ;}
Set {age = value ;}
}
}
principle:
an object is sent to the server in JSON format. After the server implements the icallbackeventhandler interface, it overwrites the getcallbackresult and raisecallbackevent methods, deserialize json in the raisecallbackevent method
and return the result to the client in getcallbackresult. I will try again later. You are welcome to discuss it!