ASP.NET of AJAX+JSON implements object invocation
- 2020-05-07 19:31:47
- OfStack
Client:
Server:
Users class
Principle:
Use JSON to send 1 object to the server, the server through the implementation of ICallbackEventHandler interface, overwrite GetCallbackResult and RaiseCallbackEvent methods, in the callback, in the RaiseCallbackEvent method
Deserialize JSON in GetCallbackResult and return the result to the client.
<%@ 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 Send 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 />
The user name :<input id="TxtNum1" type="text" />
<br />
The server :<input id="TxtRegister" type="text" /><br />
<button id="SumBtn" type="button" onclick="CallServer()"> The login </button>
</div>
</form>
</body>
</html>
Server:
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)
{
// The callback GetRegister methods
string CallBackFun = Page.ClientScript.GetCallbackEventReference(this,"arg","GetRegister","context");
// create ServerSum methods , When the client calls it , Will the callback GetRegister methods , Pass the parameter to RaiseCallbackEvent(string eventArgument ), At last,
//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 of the
public string GetCallbackResult()
{
return " The server : hello , Your username :" + u.Name + " Your age is " + u.Age;
}
public void RaiseCallbackEvent(string eventArgument)
{
JavaScriptSerializer js = new JavaScriptSerializer();
u =js.Deserialize<Users>(eventArgument);
}
#endregion
}
Users class
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
/// <summary>
///User Summary of
/// </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:
Use JSON to send 1 object to the server, the server through the implementation of ICallbackEventHandler interface, overwrite GetCallbackResult and RaiseCallbackEvent methods, in the callback, in the RaiseCallbackEvent method
Deserialize JSON in GetCallbackResult and return the result to the client.