ASP. NET (Ajax + JSON) for object calling

Source: Internet
Author: User
Tags call back

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!

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.