在做一个项目,提出这个一个需求。今天做了下,分享给大家。
jquery 代码:
var stroid = $("#storeid").val(); $.ajax({ type: "POST", url: "jyz_mon.ashx", data: "stroid=" + stroid, success: function (msg) { // alert("Data Saved: " + msg); $("#jyzdetail").html(msg); } });
一般处理程序代码(jyz_mon.ashx) :
public class jyz_mon : IHttpHandler { private readonly JyzMonitoringBll _bll = new JyzMonitoringBll(); public void ProcessRequest(HttpContext context) { var str =_bll.GetStationDetailInfoByCityCode(context.Request["storeid"]); context.Response.Write(str); } public bool IsReusable { get { return false; } } }
GetStationDetailInfoByCityCode 方法:
public String GetStationDetailInfoByCityCode(string storeid) { DataSet dstest = dal.GetStationDetailList(storeid); var detailsb = new StringBuilder(); if (dstest.Tables[0].Rows.Count <= 0) { detailsb.Append("<div id=\"datashow\">"); detailsb.Append("暂无数据</div>"); return detailsb.ToString(); } DataTable table = dstest.Tables[0]; var query1 = from datarow in table.AsEnumerable() group datarow by datarow.Field<string>("STOREID") into custGroup select custGroup; detailsb.Append("<div class=\"oiling_machine\">"); //分组 foreach (var item in query1) { //组员 int count = 0; foreach (var dataRow in item) { if (count == 0) { detailsb.Append("<span><b>加油站:</b>" + dataRow.Field<string>("STORENAME") + "</span>"); detailsb.Append("<span><b>资产类型:</b>" + dataRow.Field<string>("ASSNAME") + "</span>"); detailsb.Append("<span><b>位置分类:</b>地级市市区</span>"); detailsb.Append("<span><b>销售规模:</b>" + dataRow.Field<string>("SALESSCALENAME") + "</span>"); detailsb.Append("<span><b>星级:</b>" + dataRow.Field<string>("STARNAME") + "</span>"); detailsb.Append("<span><b>油枪数量:</b>" + table.Rows.Count + "</span>"); detailsb.Append("<ul class=\"o_m_all\">"); } count++; } } detailsb.Append("</ul>"); detailsb.Append("<div class=\"float_clear\">"); detailsb.Append("</div>"); detailsb.Append("</div>"); return detailsb.ToString(); }
如果想每隔多少时间调一次,可以在js 加上setInterval()函数,如下:
setInterval("reload()", 10000); function reload() { var stroid = $("#storeid").val(); $.ajax({ type: "POST", url: "jyz_mon.ashx", data: "stroid=" + stroid, success: function (msg) { // alert("Data Saved: " + msg); $("#jyzdetail").html(msg); } }); }