[英]How to Return DataTable from WebMethod using JSON and JQuery in asp.net?
I am new to JSON
.我是
JSON
新手。 I have created a sample which returns the String
from WebMethod
and assign the value returned to asp.net Label
control.我创建了一个示例,它从
WebMethod
返回String
并将返回的值分配给asp.net Label
控件。
Sample JSON returning String: 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>
In .cs file( returning String):在 .cs 文件中(返回字符串):
[WebMethod]
public static string DisplayData()
{
return DateTime.Now.ToString();
}
This works fine.这工作正常。
How to access DataTable
using JSON
and JQuery
?如何使用
JSON
和JQuery
访问DataTable
?
[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 .我想返回 DataTable 并使用 JSON & JQuery 绑定 GridView/Access
DataTable
每一行。 Please suggest me the right method to Return
DataTable
using JSON
.请建议我使用
JSON
Return
DataTable
的正确方法。
I have seen some sample using handlers
& some sample with using WebMethod
.我看过一些使用
handlers
示例和一些使用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.我将
DataTable
内容加载到字典中,将其序列化,一切正常。 You can modify the code to suit 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.在你的 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 JavaScript or jquery.有很多关于如何使用 JavaScript 或 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您可以在此处找到一个很好的示例。要使用
GridView
执行 CRUD 操作,请参阅此处的链接
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.