.Net2.0Ajax调用WebService返回Table

本文介绍了一个WebService与AJAX交互的示例,演示了如何通过WebService返回DataTable,并在前端展示数据。需要配置web.config文件支持JSON序列化。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

WebService后台代码:

  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. /// <summary>
  10. ///DataService 的摘要说明
  11. /// </summary>
  12. [WebService(Namespace = "http://tempuri.org/")]
  13. [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
  14. //若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消对下行的注释。 
  15. [System.Web.Script.Services.ScriptService]
  16. public class DataService : System.Web.Services.WebService {
  17.     public DataService () {
  18.         //如果使用设计的组件,请取消注释以下行 
  19.         //InitializeComponent(); 
  20.     }
  21.     [WebMethod]
  22.     public DataTable GetDataTable(string tableName)
  23.     {
  24.         //设定DataTable的名称
  25.         DataTable table = new DataTable(tableName);
  26.         //为该DataTable添加两列
  27.         table.Columns.Add(new DataColumn("Id",typeof(int)));
  28.         table.Columns.Add(new DataColumn("Name",typeof(string)));
  29.         //添加5行
  30.         for(int i=0;i<5;++i)
  31.         {
  32.             DataRow newRow=table.NewRow();
  33.             newRow["Id"]=i;
  34.             newRow["Name"]=string.Format("name {0}",i);
  35.             table.Rows.Add(newRow);
  36.         }
  37.         return table;
  38.     }    
  39. }

Web窗体前台代码:

  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  3. <html xmlns="http://www.w3.org/1999/xhtml">
  4. <head runat="server">
  5.     <title>无标题页</title>
  6.     <script type="text/javascript">
  7.         function btnGetDatatable_onclick()
  8.         {
  9.             DataService.GetDataTable("My Table",onSucceeded);
  10.         }
  11.         function onSucceeded(result)
  12.         {
  13.             //Sys.Debug.traceDump(result);
  14.             var idColName=result.columns[0].name;
  15.             var nameColName=result.columns[1].name;
  16.             //得到DataTable中的行集合。
  17.             var rows=result.rows;
  18.             //创建表格头。
  19.             var builder=new Sys.StringBuilder("<table border=1>");
  20.             builder.append(String.format("<tr><td>{0}</td><td>{1}</td></tr>",idColName,nameColName));
  21.             //创建表格内容
  22.             for(var rowIndex=0;rowIndex<rows.length;++rowIndex)
  23.             {
  24.                 builder.append(String.format("<tr><td>{0}</td><td>{1}</td></tr>",
  25.                 rows[rowIndex][idColName],
  26.                 rows[rowIndex][nameColName]));
  27.             }
  28.             builder.append("</talbe>");
  29.             $get("result").innerHTML=builder.toString();
  30.         }
  31.     </script>
  32. </head>
  33. <body>
  34.     <form id="form1" runat="server">
  35.     <asp:ScriptManager ID="dataService" runat="server">
  36.         <Services>
  37.             <asp:ServiceReference Path="~/DataService.asmx" />
  38.         </Services>
  39.     </asp:ScriptManager>
  40.     <input id="btnGetDataTable" type="button" value="get Table" onclick="return btnGetDatatable_onclick()" />
  41.     <div id="result">
  42.     </div>
  43.     </form>
  44. </body>
  45. </html>

程序中要添加Microsoft.Web.Preview.dll引用并且在Asp.net 2.0里可以直接用DataTable作为返回类型了,但是需要在Web.config文件添加序列化转换器的属性。DataSet、DataTable、DataRow均有转换器。

web.config代码如下:

在web.config中的 <system.web.extensions>  <scripting>   <webServices>中添加

  1. <jsonSerialization> 
  2.           <converters> 
  3.             <add name="DataSetConverter" type="Microsoft.Web.Preview.Script.Serialization.Converters.DataSetConverter, Microsoft.Web.Preview"/> 
  4.             <add name="DataRowConverter" type="Microsoft.Web.Preview.Script.Serialization.Converters.DataRowConverter, Microsoft.Web.Preview"/> 
  5.             <add name="DataTableConverter" type="Microsoft.Web.Preview.Script.Serialization.Converters.DataTableConverter, Microsoft.Web.Preview"/> 
  6.           </converters> 
  7. </jsonSerialization> 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值