如何从WebMethod中使用JSON和JQuery在asp.net中返回DataTable? [英] How to Return DataTable from WebMethod using JSON and JQuery in asp.net?
问题描述
我是新的 JSON
。我创建了一个样本,它从 WebMethod
返回 String
,并将返回的值分配给 asp .net标签
控件。
示例JSON返回字符串:
< asp:Content ID =Content1ContentPlaceHolderID =HeadContentRunat =Server>
< script type =text / javascriptsrc =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);
},
错误:function(result){
alert(Error);
}
});
});
< / script>
< / asp:Content>
< asp:Content ID =Content2ContentPlaceHolderID =MainContentRunat =Server>
< label id =lbltxtrunat =server>< / label>
< / asp:Content>
在.cs文件(返回String)中:
[WebMethod]
public static string DisplayData()
{
return DateTime.Now.ToString();
}
这工作正常。
如何访问 DataTable
使用 JSON
和 JQuery
?
$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $
$ b $ static $新的DataTable();
return dt.GetData();
}
我想要返回DataTable并绑定GridView / Access每个一行 DataTable
使用JSON& JQuery 。请建议我使用返回
DataTable
使用 JSON
。
我已经看到一些使用处理程序
&一些使用 WebMethod
的示例。哪一个使用?
相比之下有什么好处。
帮助欣赏!
这是我通常如何做的。我将datatable内容加载到字典中,序列化它,一切正常。你可以修改代码以满足您的需要。
[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)
{
字典< string,object> dict = new Dictionary< string,object>();
foreach(ds.Tables中的DataTable dt){
object [] arr = new object [dt.Rows.Count + 1]; (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',
数据:{},
contentType:application / json; charset = utf-8,
dataType:'json',
success:function(data){
var objdata = $ .parseJSON(data.d);
//现在遍历此对象的内容并加载gridview
}
});
有很多关于如何使用java脚本或jquery加载网格视图的教程。这至少会给你一个起点。你可以找到一个很好的例子 here 。要使用gridview进行CRUD操作,请参阅链接 here
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.
Sample JSON returning String:
<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>
In .cs file( returning String):
[WebMethod]
public static string DisplayData()
{
return DateTime.Now.ToString();
}
This works fine.
How to access DataTable
using JSON
and JQuery
?
[WebMethod]
public static DataTable DisplayData()
{
DataTable dt = new DataTable();
return dt.GetData();
}
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
.
I have seen some sample using handlers
& some sample with using WebMethod
. Which one to use?
What are the Benefits one over the other.
Help Appreciated!
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);
}
On your 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
}
});
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
这篇关于如何从WebMethod中使用JSON和JQuery在asp.net中返回DataTable?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!