最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

ASP.NET(AJAX+JSON)實現對象調用

 更新時間:2010年01月13日 23:22:58   作者:  
最近開始學習ASP.NET AJAX 搞了兩天學了點皮毛,貼點代碼 日后方便自己查看.
客戶端:
復制代碼 代碼如下:

<%@ 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發(fā)送對象
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");
//創(chuàng)建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向服務器發(fā)送一個對象,服務器通過實現 ICallbackEventHandler接口后,重寫GetCallbackResult和RaiseCallbackEvent方法,在回調的時候,在RaiseCallbackEvent方法
中反序列化JSON,并在GetCallbackResult把結果返回給客戶端.具體的我以后有空再補充,歡迎大家討論!

相關文章

最新評論

封丘县| 兴隆县| 禹城市| 建瓯市| 茌平县| 石阡县| 大悟县| 棋牌| 新巴尔虎左旗| 大理市| 永丰县| 巴青县| 银川市| 集安市| 化德县| 宜丰县| 米易县| 永川市| 涞源县| 广河县| 西城区| 永济市| 潞城市| 盘锦市| 宜兰县| 梁山县| 交城县| 卢龙县| 历史| 吴旗县| 乐至县| 通榆县| 黄山市| 浦江县| 醴陵市| 鄂尔多斯市| 盐亭县| 桦甸市| 茶陵县| 水城县| 皋兰县|