如何在asp.net中使用JSON和JQuery从返回的WebMethod数据表? [英] How to Return DataTable from WebMethod using JSON and JQuery in asp.net?

查看:124
本文介绍了如何在asp.net中使用JSON和JQuery从返回的WebMethod数据表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来的 JSON 。我创建了返回字符串样本的WebMethod 并分配值恢复到 ASP .NET标签控制。

I am new to JSON. I have created a sample which returns the String from WebMethod and assign the value returned to asp.net Label control.

样品返回JSON字符串:

<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" Runat="Server">
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>
<script type="text/javascript">
    $(document).ready(function() {
        $.ajax({
            type: "POST",
            contentType: "application/json; charset=utf-8",
            url: "JSONSample.aspx/DisplayData",
            data: "{}",
            dataType: "json",
            success: function(data) {
                //alert("hi");
                $("#ctl00_MainContent_lbltxt").text(data.d);
            },
            error: function(result) {
                alert("Error");
            }
        });
    });
</script>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" Runat="Server">

<label id="lbltxt" runat="server"></label>
</asp:Content>

在cs文件(返回字符串):

[WebMethod]
    public static string DisplayData()
    {
        return DateTime.Now.ToString();
    }

这工作得很好。

如何访问数据表使用 JSON JQuery的

[WebMethod]
    public static DataTable DisplayData()
    {
        DataTable dt = new DataTable();
        return dt.GetData();
    }

我要的返回DataTable,并绑定在GridView /访问使用JSON和放大器的DataTable 的每一行; JQuery的。请建议我正确的方法返回 数据表使用 JSON

I want to return the DataTable and Bind the GridView/Access each row of DataTable using JSON & JQuery. Please suggest me the right method to Return DataTable using JSON.

我见过使用处理一些样本&安培;一些示例使用的WebMethod 。使用哪一个?

I have seen some sample using handlers & some sample with using WebMethod. Which one to use?

有什么好处一个比其他。

What are the Benefits one over the other.

帮助鸭preciated!

Help Appreciated!

推荐答案

下面是如何我通常做它。我的数据表中的内容加载到一个字典,序列化和一切works.You可以修改code,以适应您的需求。

Here is how I normally do it.I load the datatable contents into a dictionary ,serialize it and everything works.You can modify the code to suit to your needs.

    [WebMethod]
    public string GetQueryInfo()
   {
    String daresult = null;
    DataTable yourDatable = new DataTable();
    DataSet ds = new DataSet();
    ds.Tables.Add(yourDataTable);
    daresult = DataSetToJSON(ds);
    return daresult;
    }


    public string DataSetToJSON(DataSet ds)
   {

    Dictionary<string, object> dict = new Dictionary<string, object>();
   foreach (DataTable dt in ds.Tables) {
    object[] arr = new object[dt.Rows.Count + 1];

    for (int i = 0; i <= dt.Rows.Count - 1; i++) {
        arr[i] = dt.Rows[i].ItemArray;
    }

    dict.Add(dt.TableName, arr);
    }

   JavaScriptSerializer json = new JavaScriptSerializer();
    return json.Serialize(dict);
   }

在ASPX。

                     $.ajax({
                         type: "POST",
                         url: 'Webservices/GetQueryInfo',
                         data: {},
                         contentType: "application/json; charset=utf-8",
                         dataType: 'json',

                         success: function (data) {
                          var objdata = $.parseJSON(data.d);
                       // now iterate through this object's contents and load your gridview
                          }

                     });

有关于如何使用Java脚本或jquery.This将至少给你一个开始point.You可以找到一个很好的例子的此处。要做到与GridView的CRUD操作见链接<一个href=\"http://www.dotnetfunda.com/articles/show/1465/edit-delete-paging-in-gridview-using-jquery-simplemethod-part-1\">here

There are many tutorials on how to load a grid view using java script or jquery.This will at least give you a starting point.You can find a nice example here.To do CRUD operations with the gridview see link here

这篇关于如何在asp.net中使用JSON和JQuery从返回的WebMethod数据表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆