ajax 客户端和服务器datatale转化

首先在Web站点中添加了对Microsoft.Web.Preview.dll程序集的引用(将该程序集拷贝到Web站点的/bin文件夹下)

<% @ Page Language = " C# "  AutoEventWireup = " true "  CodeFile = " Default.aspx.cs "  Inherits = " _Default "   %>

<! DOCTYPE html PUBLIC  " -//W3C//DTD XHTML 1.1//EN "   " http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd " >
< html xmlns = " http://www.w3.org/1999/xhtml " >
< head runat = " server " >
    
< title > Untitled Page </ title >

    
< script type = " text/javascript " >
        function btnGetDataTable_onclick()
         

              PageMethods.GetDataTable(
"My Table", onSucceeded);

        }


        function onSucceeded(result) 
        
{  

            
// 得到两列的名称

            var idColName 
= result.columns[0].name;

            var nameColName 
= result.columns[1].name;

            

            
// 得到DataTable中的行集合

            var rows 
= result.rows; 

            
// 创建表格头

            var builder 
= new Sys.StringBuilder("<table border=1>");

                 builder.append( String.format(
"<tr><td>{0}</td><td>{1}</td></tr>",idColName, nameColName ) ); 

            
// 创建表格内容

            
for (var rowIndex = 0; rowIndex < rows.length; ++ rowIndex) {

                builder.append(

                    String.format(

                        
"<tr><td>{0}</td><td>{1}</td></tr>",

                        rows[rowIndex][idColName],

                        rows[rowIndex][nameColName]

                    )

                );

            }
 

         builder.append(
"</table>"); 

    
// 显示表格

    $
get("result").innerHTML = builder.toString();

}





    
</ script >

</ head >
< body >
    
< form id = " form1 "  runat = " server " >
        
< asp:ScriptManager ID = " ScriptManager1 "  EnablePageMethods = " true "   runat = " server " >
         
        
</ asp:ScriptManager >
        
< div >
            
< input id = " btnGetDataTable "  type = " button "  value = " Get DataTable "  onclick = " return btnGetDataTable_onclick() "   />
            
< div id = " result " >
            
</ div >
        
</ div >
    
</ form >
</ body >
</ html >
using  System;
using  System.Data;
using  System.Configuration;
using  System.Web;
using  System.Web.Security;
using  System.Web.UI;
using  System.Web.UI.WebControls;
using  System.Web.UI.WebControls.WebParts;
using  System.Web.UI.HtmlControls;
 

public   partial   class  _Default : System.Web.UI.Page 
{
    
protected void Page_Load(object sender, EventArgs e)
    
{

    }

    
    [System.Web.Services.WebMethod]
    
public static DataTable GetDataTable(string tableName)
    
{

        
// 设定DataTable的名称

        DataTable table 
= new DataTable(tableName);



        
// 为该DataTable添加两列

        table.Columns.Add(
new DataColumn("Id"typeof(int)));

        table.Columns.Add(
new DataColumn("Name"typeof(string)));



        
// 添加5行

        
for (int i = 0; i < 5++i)
        
{

            DataRow newRow 
= table.NewRow();

            newRow[
"Id"= i;

            newRow[
"Name"= string.Format("name {0}", i);



            table.Rows.Add(newRow);

        }

        
return table;

    }

}
<? xml version = " 1.0 " ?>
< configuration >
  
< configSections >
    
< sectionGroup name = " system.web.extensions "  type = " System.Web.Configuration.SystemWebExtensionsSectionGroup, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35 " >
      
< sectionGroup name = " scripting "  type = " System.Web.Configuration.ScriptingSectionGroup, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35 " >
        
< section name = " scriptResourceHandler "  type = " System.Web.Configuration.ScriptingScriptResourceHandlerSection, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35 "  requirePermission = " false "  allowDefinition = " MachineToApplication " />
        
< sectionGroup name = " webServices "  type = " System.Web.Configuration.ScriptingWebServicesSectionGroup, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35 " >
          
< section name = " jsonSerialization "  type = " System.Web.Configuration.ScriptingJsonSerializationSection, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35 "  requirePermission = " false "  allowDefinition = " Everywhere "   />
          
< section name = " profileService "  type = " System.Web.Configuration.ScriptingProfileServiceSection, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35 "  requirePermission = " false "  allowDefinition = " MachineToApplication "   />
          
< section name = " authenticationService "  type = " System.Web.Configuration.ScriptingAuthenticationServiceSection, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35 "  requirePermission = " false "  allowDefinition = " MachineToApplication "   />
        
</ sectionGroup >
      
</ sectionGroup >
    
</ sectionGroup >
  
</ configSections >

  
< system.web >
    
< pages >
      
< controls >
        
< add tagPrefix = " asp "   namespace = " System.Web.UI "  assembly = " System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35 " />
      
</ controls >
    
</ pages >
    
<!--
          Set compilation debug
= " true "  to insert debugging
          symbols into the compiled page. Because 
this
          affects performance, 
set   this  value to  true  only
          during development.
    
-->
    
< compilation debug = " false " >
      
< assemblies >
        
< add assembly = " System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35 " />
      
</ assemblies >
    
</ compilation >

    
< httpHandlers >
      
< remove verb = " * "  path = " *.asmx " />
      
< add verb = " * "  path = " *.asmx "  validate = " false "  type = " System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35 " />
      
< add verb = " * "  path = " *_AppService.axd "  validate = " false "  type = " System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35 " />
      
< add verb = " GET,HEAD "  path = " ScriptResource.axd "  type = " System.Web.Handlers.ScriptResourceHandler, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35 "  validate = " false " />
    
</ httpHandlers >

    
< httpModules >
      
< add name = " ScriptModule "  type = " System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35 " />
    
</ httpModules >
  
</ system.web >

  
< system.web.extensions >
    
< scripting >
      
< webServices >
        
<!--  Uncomment  this  line to customize maxJsonLength and add a custom converter  -->
        
<!--  客户端与服务器datatable转化重点 -->
        
< jsonSerialization >

          
< converters >

            
< add name = " DataSetConverter "  type = " Microsoft.Web.Preview.Script.Serialization.Converters.DataSetConverter, Microsoft.Web.Preview, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35 " />

            
< add name = " DataRowConverter "  type = " Microsoft.Web.Preview.Script.Serialization.Converters.DataRowConverter, Microsoft.Web.Preview, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35 " />

            
< add name = " DataTableConverter "  type = " Microsoft.Web.Preview.Script.Serialization.Converters.DataTableConverter, Microsoft.Web.Preview, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35 " />

          
</ converters >

        
</ jsonSerialization >
        
<!--  客户端与服务器datatable转化重点结束 -->

        
<!--  Uncomment  this  line to enable the authentication service. Include requireSSL = " true "   if  appropriate.  -->
        
<!--
        
< authenticationService enabled = " true "  requireSSL  =   " true|false " />
      
-->

        
<!--  Uncomment these lines to enable the profile service. To allow profile properties to be retrieved
           and modified 
in  ASP.NET AJAX applications, you need to add each property name to the readAccessProperties and
           writeAccessProperties attributes. 
-->
        
<!--
      
< profileService enabled = " true "
                      readAccessProperties
= " propertyname1,propertyname2 "
                      writeAccessProperties
= " propertyname1,propertyname2 "   />
      
-->
      
</ webServices >
      
<!--
      
< scriptResourceHandler enableCompression = " true "  enableCaching = " true "   />
      
-->
    
</ scripting >
  
</ system.web.extensions >

  
< system.webServer >
    
< validation validateIntegratedModeConfiguration = " false " />
    
< modules >
      
< add name = " ScriptModule "  preCondition = " integratedMode "  type = " System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35 " />
    
</ modules >
    
< handlers >
      
< remove name = " WebServiceHandlerFactory-Integrated "   />
      
< add name = " ScriptHandlerFactory "  verb = " * "  path = " *.asmx "  preCondition = " integratedMode "
           type
= " System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35 " />
      
< add name = " ScriptHandlerFactoryAppServices "  verb = " * "  path = " *_AppService.axd "  preCondition = " integratedMode "
           type
= " System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35 " />
      
< add name = " ScriptResource "  preCondition = " integratedMode "  verb = " GET,HEAD "  path = " ScriptResource.axd "  type = " System.Web.Handlers.ScriptResourceHandler, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35 "   />
    
</ handlers >
  
</ system.webServer >
</ configuration >
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值