The server then constructs the GridView into a DataTable, adds a row to the DataTable, binds to the GridView, and sends it back to the client ...
Can it be simpler?
Using AJAX data to request data is usually a simple format, such as String, with less information. Of course, you can also request XML, but XML data redundancy is much more cumbersome to take to client processing than JSON.
Can it be simpler?
These problems, if the DataTable and JSON types can be easily converted to each other, 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.
Since the benefits are so much, let's go to the code.
Foreground code:
Copy Code code as follows:
<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 + "he\nll\" "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>
Background code:
<summary>
Copy Code code as follows:
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)
{
JavaScriptSerializer 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;
}
And then attach a download file, feel useful to download and see
Json.zip