zoukankan      html  css  js  c++  java
  • 使用JavaScriptConverter实现返回DataTable对象

    使用JavaScriptConverter
    •复杂类型作为返回值时可能会出现问题
    –循环引用
    •解决方案
    –使用自定义数据类型封装复杂类型
    –在web.config里定义Converter

    aspx
        <form id="form1" runat="server">
            
    <asp:ScriptManager ID="ScriptManager1" runat="server">
                
    <Services>
                    
    <asp:ServiceReference Path="DataTableService.asmx" InlineScript="true" />
                
    </Services>
            
    </asp:ScriptManager>
            
            
    <input type="button" value="Get DataTable" onclick="getDataTable();" />
            
            
    <div id="result"></div>
            
            
    <script language="javascript" type="text/javascript">
                function getDataTable()
                {
                    DataTableService.GetDataTable(onSucceeded, onFailed);
                }
                
                function onSucceeded(result)
                {
                    
    // alert(result);
                    var sb = new Sys.StringBuilder("<table border='1'>");
                    sb.append(
    "<tr><td>ID</td><td>Text</td></tr>");
                    
    for (var i = 0; i < result.rows.length; i++)
                    {
                        sb.append(
                            String.format(
                                
    "<tr><td>{0}</td><td>{1}</td></tr>",
                                result.rows[i][
    "ID"],
                                result.rows[i].Text));
                    }
                    sb.append(
    "</table>");
                    
                    $
    get("result").innerHTML = sb.toString();
                }
                
                function onFailed(error)
                {
                    alert(error.get_message());
                }
            
    </script>
        
    </form>

    DataTableService.asmx
    <%@ WebService Language="C#" Class="DataTableService" %>

    using System;
    using System.Web;
    using System.Web.Services;
    using System.Web.Services.Protocols;
    using System.Web.Script.Services;
    using System.Data;

    [WebService(Namespace 
    = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo 
    = WsiProfiles.BasicProfile1_1)]
    [ScriptService]
    public class DataTableService  : System.Web.Services.WebService
    {
        [WebMethod]
        
    public DataTable GetDataTable()
        {
            DataTable dt 
    = new DataTable();
            dt.Columns.Add(
    new DataColumn("ID"typeof(int)));
            dt.Columns.Add(
    new DataColumn("Text"typeof(string)));

            Random random 
    = new Random(DateTime.Now.Millisecond);
            
    for (int i = 0; i < 10; i++)
            {
                dt.Rows.Add(i, random.Next().ToString());
            }

            
    return dt;
        }

    }

    web.config中的</system.web>后面加入以下代码,指定要用到的Converter,这里其中的一个“BoyConverter”是一个自定义的Converter
      <system.web.extensions>
        
    <scripting>
          
    <webServices>
          
    <!-- Uncomment this line to customize maxJsonLength and add a custom converter -->
          
          
    <jsonSerialization>
            
    <converters>
                        
    <add name="DataSetConverter" type="Microsoft.Web.Preview.Script.Serialization.Converters.DataSetConverter, Microsoft.Web.Preview" />
                        
    <add name="DataRowConverter" type="Microsoft.Web.Preview.Script.Serialization.Converters.DataRowConverter, Microsoft.Web.Preview" />
                        
    <add name="DataTableConverter" type="Microsoft.Web.Preview.Script.Serialization.Converters.DataTableConverter, Microsoft.Web.Preview" />
                        
    <add name="BoyConverter" type="Converter.BoyConverter, App_Code" />
                    
    </converters>
          
    </jsonSerialization>
          
          
    <!-- 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>
    最后还要引入Microsoft.Web.Preview.dll才能正确使用
  • 相关阅读:
    cpp 模版函数
    叉积
    利用scrollTop 制作图片无缝滚动
    事件绑定和时间取消
    闭包写法
    增加类,删除类,查找类
    获取元素到页面上的位置
    在IE8中如何通过javascripts改变<style />中的内容?
    有关app的一些小知识
    获取页面高宽知识
  • 原文地址:https://www.cnblogs.com/timy/p/1173142.html
Copyright © 2011-2022 走看看