当前位置:编程学习 > C#/ASP.NET >>

Jquery Ajax传送和接收DataTable的asp.net代码

本文章讲一下关于jquery ajax传送和接收datatable的asp教程.net代码哦,下面我来举例说明一下ajax传送和接收datatable的实例吧。

asp.net教程代码

简单举例说明

webservices:

[webmethod(true)]
  [scriptmethod(responseformat = responseformat.xml)] 
  public string compe易做图(string sl_id)
  {
  dataset ds;
  slcompe易做图 slcompe易做图 =new slcompe易做图();
  ds = slcompe易做图.getslcompe易做图(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("加载时出错,详情:"+data.status+">>>"+data.statustext);
  },  
  success:function(getxml) {
  tabledecider.removechild(tbodydecider);
  $$(getxml).find("table").each(function(i){//xml 目录id
  var name=$$(this).children("stakeholder").text(); //取文本


asp.netx文件

void ihttphandler.processrequest(httpcontext context)
        {
           // context.response.contenttype = "text/json";

            int bids=0;
            int.tryparse(context.request.querystring["bid"].tostring(), out bids);//项目id

            int pid = 0;
            int.tryparse(context.request.querystring["productid"].tostring(),out pid);//产品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教程address"); //address
                            jsont.writevalue(itemrow["psaddress"]);

                            jsont.writepropertyname("quantity");//数量
                            jsont.writevalue(itemrow["quantity"]);

                            jsont.writepropertyname("unit");//单位
                            jsont.writevalue(itemrow["unit"]);

                            jsont.writepropertyname("note");//备注
                            jsont.writevalue(itemrow["note"]);

                            jsont.writepropertyname("contactman");//联系人
                            jsont.writevalue(itemrow["contactman"]);

                            jsont.writepropertyname("contactmobile");//联系人手机
                            jsont.writevalue(itemrow["contactmobile"]);

                            jsont.writepropertyname("bid");//项目id
                            jsont.writevalue(itemrow["bid"]);

                            jsont.writepropertyname("productid");//产品id
                            jsont.writevalue(itemrow["productid"]);
                            jsont.writeendobject();
                        }
                        jsont.writeendarray();
                    }
                }
                else
                {
                    context.response.output.write("[]");
                }
            }
        }


实例代码二

/// 页面加载时
/// </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)
{
网页特效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转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代码

 

<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 + "henll"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>

如果datatable与json类型可以方便的相互转换,都可以迎刃而解了。
优点:1)避免不必要的回传;
2)精简异步请求数据的大小 ;
3)解决数据量较大时,数据发送与接收繁琐的问题。

 

补充:asp.net教程,.Net开发
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,