国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > .NET > 正文

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

2024-07-10 12:42:43
字體:
來源:轉載
供稿:網友
客戶端:
代碼如下:
<%@ 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方法
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 南和县| 汽车| 鲜城| 沂南县| 成都市| 奎屯市| 桃江县| 汉沽区| 海淀区| 新河县| 黔东| 江津市| 嘉祥县| 皮山县| 天等县| 德兴市| 宁安市| 咸阳市| 榆树市| 虞城县| 安塞县| 大名县| 新闻| 五家渠市| 兴仁县| 台南县| 高雄县| 政和县| 孝昌县| 琼海市| 凤城市| 吉木乃县| 涿鹿县| 昌邑市| 米泉市| 德安县| 沅陵县| 清镇市| 麻阳| 独山县| 安阳市|