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>
 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章