chenweifu 2010-09-12
代码如下:
<html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <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() { //点击botton1 $("#botton1").click(function() { var url = "default.aspx?ajax=1"; var dtb = generateDtb(); //序列化对象 var postdata = JSON.stringify(dtb); //异步请求 $.post(url, { json: postdata }, function(json) { createTable(json); }, "json") }); }); //生成DataTable对象 function generateDtb() { var dtb = new Array(); for (var i = 0; i < 10; 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; } //显示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> </head> <body> <form id="form1" runat="server"> <div> <input id="botton1" type="button" value="button" /> <div id="back"> </div> </div> </form> </body> </html>
代码如下:
/// 页面加载时 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void Page_Load(object sender, EventArgs e) { //判断是否异步请求 if (Request.QueryString["ajax"] == "1") { ProcessRequest(); } } /// <summary> /// 处理异步请求 /// </summary> 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(); } /// <summary> /// DataTable转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转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; }
结束数据方法的参数,该如何定义?-- 集合为自定义实体类中的结合属性,有几个实体类,改变下标就行了。<input id="add" type="button" value="新增visitor&quo
本文实例讲述了php+ ajax 实现的写入数据库操作。分享给大家供大家参考,具体如下:。<input class="tel" type="text" placeholder="请输入您的手机号码&q