This article is about the ASP tutorial on jquery Ajax transfer and receive DataTable. NET code Oh, let me give you an example of Ajax sending and receiving a DataTable.
asp.net tutorial Code
A simple example shows
WebServices:
[WebMethod (True)]
[Scriptmethod (Responseformat = responseformat.xml)]
public string Competitor (String sl_id)
{
DataSet ds;
Slcompetitor slcompetitor =new slcompetitor ();
ds = Slcompetitor.getslcompetitor (Int.parse (sl_id));
return Ds.getxml ();
}
Js:
$$.ajax ({
Type: "Post",
URL: ".. /webservices/advquerycarwebservice.asmx/deciderdetai ",
Data: "{sl_id:" +sl_id+ "}",
DataType: "XML",
ContentType: "Application/json",
Error:function (data) {
Alert ("Error loading, Details:" +data.status+ ">>>" +data.statustext);
},
Success:function (Getxml) {
Tabledecider.removechild (Tbodydecider);
$$ (Getxml). Find ("table"). each (function (i) {//xml directory ID
var name=$$ (this). Children ("stakeholder"). Text (); Fetching text
Asp.netx file
void Ihttphandler.processrequest (HttpContext context)
{
Context.response.contenttype = "Text/json";
int bids=0;
Int.tryparse (context.request.querystring["bid"].tostring (), out Bids);//Project ID
int pid = 0;
Int.tryparse (context.request.querystring["ProductID"].tostring (), out PID);//Product ID
If (bids!= 0 && pid!= 0)
{
DataTable dt = logic.bidaddress.getbidaddresstobidandproductidlist (bids, PID);
if (dt.rows.count!= 0)
{
using (jsonwriter Jsont = new JsonTextWriter (context.response.output))
{
Jsont.writestartarray ();
foreach (DataRow itemrow in Dt.rows)
{
Jsont.writestartobject ();
Jsont.writepropertyname ("Baid");//table ID
Jsont.writevalue (itemrow["Baid"]);
Jsont.writepropertyname ("PS Tutorial Address"); Address
Jsont.writevalue (itemrow["psaddress"]);
Jsont.writepropertyname ("Quantity");//Quantity
Jsont.writevalue (itemrow["Quantity"]);
Jsont.writepropertyname ("unit");
Jsont.writevalue (itemrow["unit"]);
Jsont.writepropertyname ("note");
Jsont.writevalue (itemrow["note"]);
Jsont.writepropertyname ("Contactman");//Contact
Jsont.writevalue (itemrow["Contactman"]);
Jsont.writepropertyname ("Contactmobile");/Contact Phone
Jsont.writevalue (itemrow["Contactmobile"]);
Jsont.writepropertyname ("bid");//Project ID
Jsont.writevalue (itemrow["bid"]);
Jsont.writepropertyname ("ProductID");//Product ID
Jsont.writevalue (itemrow["ProductID"]);
Jsont.writeendobject ();
}
Jsont.writeendarray ();
}
}
Else
{
Context.response.output.write ("[]");
}
}
}
Instance Code two
When the page loads
</summary>
<param name= "Sender" ></param>
<param name= "E" ></param>
protected void Page_Load (object sender, EventArgs e)
{
To determine whether an asynchronous request
if (request.querystring["ajax"] = = "1")
{
ProcessRequest ();
}
}
<summary>
Processing asynchronous requests
</summary>
private void ProcessRequest ()
{
Response.ContentType = "text/html";
String json = request.form["JSON"];
Deserializing a DataTable
DataTable NEWDTB = JSON2DTB (JSON);
Serializing a DataTable as JSON
String back = Dtb2json (NEWDTB);
Response.Write (back);
Response.End ();
}
<summary>
DataTable Turn JSON
</summary>
<param name= "DTB" ></param>
<returns></returns>
private String Dtb2json (DataTable DTB)
{
Web Effects Serializer JSS = new JavaScriptSerializer ();
ArrayList dic = new ArrayList ();
foreach (DataRow row in dtb.rows)
{
Dictionary<string, object> drow = new dictionary<string, object> ();
foreach (DataColumn col in Dtb.columns)
{
Drow.add (Col.columnname, Row[col.columnname]);
}
Dic.add (drow);
}
Return Jss.serialize (DIC);
}
<summary>
JSON Goto DataTable
</summary>
<param name= "JSON" ></param>
<returns></returns>
Private DataTable JSON2DTB (string json)
{
JavaScriptSerializer JSS = new JavaScriptSerializer ();
ArrayList dic = jss.deserialize<arraylist> (JSON);
DataTable DTB = new DataTable ();
if (Dic.count > 0)
{
foreach (dictionary<string, object> drow in dic)
{
if (dtb.columns.count = 0)
{
foreach (string key in Drow.keys)
{
Dtb.columns.add (Key, Drow[key].gettype ());
}
}
DataRow row = Dtb.newrow ();
foreach (string key in Drow.keys)
{
Row[key] = Drow[key];
}
Dtb.rows.add (row);
}
}
return DTB;
}
jquery Code
<title></title>
<script type= "Text/javascript" src= "Js/jquery-1.4.2.min.js" ></script>
<script type= "Text/javascript" src= "Js/json2.js" ></script>
<script type= "Text/javascript" >
OnLoad
$ (function () {
Click Botton1
$ ("#botton1"). Click (function () {
var url = "Default.aspx?ajax=1";
var dtb = GENERATEDTB ();
Serializing an Object
var postdata = json.stringify (DTB);
Asynchronous request
$.post (URL, {json:postdata}, function (JSON) {
CreateTable (JSON);
}, "JSON")
});
});
Generating a DataTable Object
function Generatedtb () {
var dtb = new Array ();
for (var i = 0; i < i++) {
var row = new Object ();
Row.col1 = i;
Row.col2 = i% 2 = 0? True:false;
Row.col3 = i + "HENLL" "Ow";
Dtb.push (row);
}
return DTB;
}
Displaying data in JSON
function CreateTable (JSON) {
var table = $ ("<table border= ' 1 ' ></table>");
for (var i = 0; i < json.length; i++) {
O1 = Json[i];
var row = $ ("<tr></tr>");
For (key in O1) {
var td = $ ("<td></td>");
Td.text (O1[key].tostring ());
Td.appendto (row);
}
Row.appendto (table);
}
Table.appendto ($ ("#back"));
}
</script>
<body>
<form id= "Form1" runat= "Server" >
<div>
<input id= "Botton1" type= "button" value= "button"/>
<div id= "Back" >
</div>
</div>
</form>
</body>
If the DataTable and JSON types can be easily converted to each other, it can be solved.
Advantages: 1 Avoid unnecessary return;
2 Simplify the size of asynchronous request data;
3 to solve the large amount of data, the data sent and received cumbersome problems.