Home > Web Front-end > JS Tutorial > Code for transmitting and receiving DataTable using Jquery Ajax under Asp.net_jquery

Code for transmitting and receiving DataTable using Jquery Ajax under Asp.net_jquery

WBOY
Release: 2016-05-16 18:19:47
Original
1146 people have browsed it

The server then deconstructs the GridView into a DataTable, adds a row to the DataTable, binds it to the GridView, and then sends it back to the client...
Can it be simpler?
When using Ajax data to request data, it is usually in a simple format, such as String, with less information. Of course, you can also request XML back, but XML data is much redundant, and getting it to the client for processing is much more troublesome than json.
Can it be simpler?
The above problems can be easily solved if the DataTable and JSON types can be easily converted to each other.
Advantages: 1) Avoid unnecessary postbacks;
2) Streamline the size of asynchronous request data;
3) Solve the problem of tedious data sending and receiving when the amount of data is large.
Since there are so many benefits, let’s get into the code.
Front-end code:

Copy code The code is as follows:


















Backend code:
///
Copy code Code As follows:

/// 페이지가 로드될 때
///

/// /
protected void Page_Load(object sender, EventArgs e)
{
//비동기 요청 여부 결정
if (Request. QueryString[ "ajax"] == "1")
{
ProcessRequest()
}
}
///
/// 비동기 요청 처리
///

private void ProcessRequest()
{
Response.ContentType = "text/html"
string json = Request.Form["json"] ;
//DataTable 역직렬화
DataTable newdtb = Json2Dtb(json);
//DataTable을 JSON으로 직렬화
string back = Dtb2Json(newdtb)
Response.Write(back); 🎜>Response.End();
///
/// DataTable을 Json으로
///
param name="dtb">
///
private string Dtb2Json(DataTable dtb)
{
JavaScriptSerializer jss = new JavaScriptSerializer ();
ArrayList dic = new ArrayList();
foreach(dtb.Rows의 DataRow 행)
{
Dictionary( ) ;
foreach(dtb.Columns의 DataColumn col)
{
drow.Add(col.ColumnName, row[col.ColumnName])
}
dic.Add(drow) ;
}
return jss.Serialize(dic);
///
/// Json을 DataTable로
/// 🎜>///
///
private DataTable Json2Dtb(string json)
{
JavaScriptSerializer jss = new JavaScriptSerializer();
ArrayList dic = jss.Deserialize(json);
DataTable dtb = new DataTable()
{
foreach (Dictionary drow in dic)
{
if (dtb.Columns.Count == 0)
{
foreach (drow의 문자열 키 . 키)
{
dtb.Columns.Add(key, drow[key].GetType())
}
}
DataRow row = dtb.NewRow(); foreach(drow.Keys의 문자열 키)
{
row[key] = drow[key]
}
dtb.Rows.Add(row)
}
}
return dtb;
}


다른 다운로드 파일이 첨부되어 있으니

json.zip
을 다운로드하세요.
Related labels:
source:php.cn
Previous article:Page word search based on jquery JS_jquery Next article:Implementation of Enter key to switch focus based on Jquery_jquery
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Latest Issues
Related Topics
More>
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template