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!