这个DEMO主要是展试了如何用JSON,由于有Microsot.Web.Preview.dll的支持. 对于从WEB SERVER 上返回DataSet ,DataTable ,DataRow.将变的很简单,
以下是实现的步骤.
1.先下载 Microsot.Web.Preview.dll
2.在WEB config 加上 以下代码.
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 retrieved
17 and modified in ASP.NET AJAX applications, you need to add each property name to the readAccessProperties and
18 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 <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 retrieved
17 and modified in ASP.NET AJAX applications, you need to add each property name to the readAccessProperties and
18 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>
3.新建 一个WEB SERVICES文件.
web services Code
1using System;
2using System.Collections;
3using System.Linq;
4using System.Web;
5using System.Web.Services;
6using System.Web.Services.Protocols;
7using System.Xml.Linq;
8using 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]
17public class GetDataTable : System.Web.Services.WebService
18{
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}
(注意:web services 里的[System.Web.Script.Services.ScriptService] 属性要开起来..否则会提示: 找不到web services.)
1using System;
2using System.Collections;
3using System.Linq;
4using System.Web;
5using System.Web.Services;
6using System.Web.Services.Protocols;
7using System.Xml.Linq;
8using 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]
17public class GetDataTable : System.Web.Services.WebService
18{
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}
client js code
1/**//// <reference name="MicrosoftAjax.js"/>
2
3Type.registerNamespace("ASPACE");
4
5ASPACE.GetDataTable = function(name) {
6 this._name = name;
7}
8
9ASPACE.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}
33ASPACE.GetDataTable.registerClass('ASPACE.GetDataTable',Sys.UI.Behavior);
34
35if (typeof(Sys) !== 'undefined') Sys.Application.notifyScriptLoaded();
36
(注意:在JS类里调用函数,要加"this",否则会提示:找不到函数.1/**//// <reference name="MicrosoftAjax.js"/>
2
3Type.registerNamespace("ASPACE");
4
5ASPACE.GetDataTable = function(name) {
6 this._name = name;
7}
8
9ASPACE.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}
33ASPACE.GetDataTable.registerClass('ASPACE.GetDataTable',Sys.UI.Behavior);
34
35if (typeof(Sys) !== 'undefined') Sys.Application.notifyScriptLoaded();
36
. 使用js类时.和使用.cs文件一样,先new 一个.
4. test 代码.
demo = new ASPACE.GetDataTable("test");
demo.Click_GetDataTable();
以上就是所有的代码.