asp.net (Ajax+json) Implement object invocation _ practical skills

Source: Internet
Author: User
Client:
Copy Code code 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 ">
<title></title>
<script type= "Text/jscript" >
function CallServer () {
JSON Send Object
Serversum ("{name: ' Linyijia ', Age: ' 21 '}");
}
function Getregister (RG, Contex) {
document.getElementById ("Txtregister"). Value=rg;
}
</script>

<body>
<form id= "Form1" runat= "Server" >
<div>
<br/>
User name: <input id= "TxtNum1" type= "text"/>
<br/>
Server: <input id= "Txtregister" type= "text"/><br/>
<button id= "sumbtn" type= "button" onclick= "CallServer ()" > Login </button>
</div>
</form>
</body>


Server:
Copy Code code 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)
{
Callback Getregister method
String callbackfun = Page.ClientScript.GetCallbackEventReference (This, "Arg", "Getregister", "context");
Creates the Serversum method, which, when invoked by the client, recalls the Getregister method, passes the argument to the RaiseCallbackEvent (string eventargument), and finally passes the
The GetCallbackResult () method passes the return 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 Members
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 class
Copy Code code as follows:

Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Web;
<summary>
User's summary description
</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:
Using JSON to send an object to the server, the server rewrites the GetCallbackResult and RaiseCallbackEvent methods after implementing the ICallbackEventHandler interface, and at the time of the callback, In the RaiseCallbackEvent method
, and returns the result to the client in GetCallbackResult. Specific I later have time to add, welcome to discuss!

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.