日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > asp.net >内容正文

asp.net

ASP.NET AJAX + JSON 实现对象调用

發布時間:2024/6/5 asp.net 39 豆豆
生活随笔 收集整理的這篇文章主要介紹了 ASP.NET AJAX + JSON 实现对象调用 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

最近開始學習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發送對象
??????????? 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" οnclick="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把結果返回給客戶端.具體的我以后有空再補充,歡迎大家討論!

轉載于:https://www.cnblogs.com/linyijia/archive/2010/01/13/1646716.html

總結

以上是生活随笔為你收集整理的ASP.NET AJAX + JSON 实现对象调用的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。