ASP.NET(AJAX+JSON)实现对象调用
程序员文章站
2024-03-08 12:26:52
客户端: 复制代码 代码如下: <%@ page language="c#" autoeventwireup="true" codefile="asp.neta_ja...
客户端:
<%@ 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发送对象
serversum("{name:'linyijia',age:'21'}");
}
function getregister(rg, contex) {
document.getelementbyid("txtregister").value=rg;
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<br />
用户名:<input id="txtnum1" type="text" />
<br />
服务器:<input id="txtregister" type="text" /><br />
<button id="sumbtn" type="button" onclick="callserver()">登录</button>
</div>
</form>
</body>
</html>
服务器:
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)
{
//回调getregister方法
string callbackfun = page.clientscript.getcallbackeventreference(this,"arg","getregister","context");
//创建serversum方法,在客户端调用的时候就,会回调getregister方法,把参数传给raisecallbackevent(string eventargument ),最后通过
//getcallbackresult()方法把返回值传给客户端
string registerfun = string.format("function serversum(arg,context){{{0};}}",callbackfun);
page.clientscript.registerclientscriptblock(this.gettype(),"serversum",registerfun,true);
}
string mssage = string.empty;
#region icallbackeventhandler 成员
public string getcallbackresult()
{
return "服务器:你好,你的用户名为:" + u.name + "你的年龄为" + u.age;
}
public void raisecallbackevent(string eventargument)
{
javascriptserializer js = new javascriptserializer();
u =js.deserialize<users>(eventargument);
}
#endregion
}
users类
using system;
using system.collections.generic;
using system.linq;
using system.web;
/// <summary>
///user 的摘要说明
/// </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; }
}
}
原理:
使用json向服务器发送一个对象,服务器通过实现 icallbackeventhandler接口后,重写getcallbackresult和raisecallbackevent方法,在回调的时候,在raisecallbackevent方法
中反序列化json,并在getcallbackresult把结果返回给客户端.具体的我以后有空再补充,欢迎大家讨论!
复制代码 代码如下:
<%@ 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发送对象
serversum("{name:'linyijia',age:'21'}");
}
function getregister(rg, contex) {
document.getelementbyid("txtregister").value=rg;
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<br />
用户名:<input id="txtnum1" type="text" />
<br />
服务器:<input id="txtregister" type="text" /><br />
<button id="sumbtn" type="button" onclick="callserver()">登录</button>
</div>
</form>
</body>
</html>
服务器:
复制代码 代码如下:
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)
{
//回调getregister方法
string callbackfun = page.clientscript.getcallbackeventreference(this,"arg","getregister","context");
//创建serversum方法,在客户端调用的时候就,会回调getregister方法,把参数传给raisecallbackevent(string eventargument ),最后通过
//getcallbackresult()方法把返回值传给客户端
string registerfun = string.format("function serversum(arg,context){{{0};}}",callbackfun);
page.clientscript.registerclientscriptblock(this.gettype(),"serversum",registerfun,true);
}
string mssage = string.empty;
#region icallbackeventhandler 成员
public string getcallbackresult()
{
return "服务器:你好,你的用户名为:" + u.name + "你的年龄为" + u.age;
}
public void raisecallbackevent(string eventargument)
{
javascriptserializer js = new javascriptserializer();
u =js.deserialize<users>(eventargument);
}
#endregion
}
users类
复制代码 代码如下:
using system;
using system.collections.generic;
using system.linq;
using system.web;
/// <summary>
///user 的摘要说明
/// </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; }
}
}
原理:
使用json向服务器发送一个对象,服务器通过实现 icallbackeventhandler接口后,重写getcallbackresult和raisecallbackevent方法,在回调的时候,在raisecallbackevent方法
中反序列化json,并在getcallbackresult把结果返回给客户端.具体的我以后有空再补充,欢迎大家讨论!