这个DEMO主要是展试了如何用JSON,由于有Microsot.Web.Preview.dll的支持. 对于从WEB SERVER 上返回DataSet ,DataTable ,DataRow.将变的很简单,
以下是实现的步骤.
1.先下载 Microsot.Web.Preview.dll
2.在WEB config 加上 以下代码.
1
<system.web.extensions>2
<scripting>3
<webServices>4
<!-- Uncomment this line to customize maxJsonLength and add a custom converter -->5
<jsonSerialization maxJsonLength="5000000">6
<converters>7
<add name="DataSetConverter" type="Microsoft.Web.Preview.Script.Serialization.Converters.DataSetConverter, Microsoft.Web.Preview, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>8
<add name="DataRowConverter" type="Microsoft.Web.Preview.Script.Serialization.Converters.DataRowConverter, Microsoft.Web.Preview, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>9
<add name="DataTableConverter" type="Microsoft.Web.Preview.Script.Serialization.Converters.DataTableConverter, Microsoft.Web.Preview, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>10
</converters>11
</jsonSerialization>12
<!-- Uncomment this line to enable the authentication service. Include requireSSL="true" if appropriate. -->13
<!--14
<authenticationService enabled="true" requireSSL = "true|false"/>15
-->16
<!-- Uncomment these lines to enable the profile service. To allow profile properties to be retrieved17
and modified in ASP.NET AJAX applications, you need to add each property name to the readAccessProperties and18
writeAccessProperties attributes. -->19
<!--20
<profileService enabled="true"21
readAccessProperties="propertyname1,propertyname2"22
writeAccessProperties="propertyname1,propertyname2" />23
-->24
</webServices>25
<!-- 26
<scriptResourceHandler enableCompression="true" enableCaching="true" />27
-->28
</scripting>29
</system.web.extensions>1
using System;2
using System.Collections;3
using System.Linq;4
using System.Web;5
using System.Web.Services;6
using System.Web.Services.Protocols;7
using System.Xml.Linq;8
using System.Data;9

10

/**//// <summary>11
///GetDataTable 的摘要说明12
/// </summary>13
[WebService(Namespace = "http://tempuri.org/")]14
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]15
//若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消对下行的注释。 16
[System.Web.Script.Services.ScriptService]17
public class GetDataTable : System.Web.Services.WebService18


{19

20
public GetDataTable()21

{22

23
//如果使用设计的组件,请取消注释以下行 24
//InitializeComponent(); 25
}26

27
[WebMethod]28
[System.Web.Script.Services.ScriptMethod]29
public DataTable ReturnDataTable()30

{31
BusinessLogicLayer.UserClass uc = new BusinessLogicLayer.UserClass();32
return uc.GetAllUser(1);33
}34

35
}1

/**//// <reference name="MicrosoftAjax.js"/>2

3
Type.registerNamespace("ASPACE");4

5

ASPACE.GetDataTable = function(name)
{6
this._name = name;7
}8

9

ASPACE.GetDataTable.prototype =
{10
Click_GetDataTable: function()11

{ 12
GetDataTable.ReturnDataTable(this.GetDataTable_CallBack,this.OnError);13
},14
15
GetDataTable_CallBack: function(jsonObject)16

{17
var jsonValue = Sys.Serialization.JavaScriptSerializer.serialize(jsonObject);18
var jsonTable = new System.Data.DataTable();19
var resultContent = ""; 20
eval("jsonTable = " +jsonValue);21
for(var i=0;i<jsonTable.rows.length;i++)22

{23
resultContent += jsonTable.rows[i].Memb_Name + "<br />";24
}25
$get("result").innerHTML = resultContent;26
},27
28
OnError: function(jsonObject)29

{30
alert(jsonObject);31
}32
}33
ASPACE.GetDataTable.registerClass('ASPACE.GetDataTable',Sys.UI.Behavior);34

35
if (typeof(Sys) !== 'undefined') Sys.Application.notifyScriptLoaded();36

. 使用js类时.和使用.cs文件一样,先new 一个.
4. test 代码.
demo = new ASPACE.GetDataTable("test");
demo.Click_GetDataTable();
以上就是所有的代码.
本文详细介绍了如何在ASP.NET AJAX中使用JSON进行数据交换,重点讲解了配置Microsoft.Web.Preview.dll支持的步骤,以及如何从Web服务器返回DataSet、DataTable和DataRow。通过示例代码展示了创建Web Services的方法,并在客户端JavaScript中解析返回的JSON数据。

被折叠的 条评论
为什么被折叠?



