欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

asp.net异步获取datatable并显示的实现方法

程序员文章站 2024-02-11 20:56:40
本文讲述了asp.net异步获取datatable并显示的实现方法。分享给大家供大家参考,具体如下: 上面就是结果,前台代码如下: <%@ page...

本文讲述了asp.net异步获取datatable并显示的实现方法。分享给大家供大家参考,具体如下:

asp.net异步获取datatable并显示的实现方法

上面就是结果,前台代码如下:

<%@ page language="c#" autoeventwireup="true"
codefile="methodone.aspx.cs" inherits="_default" %>
<!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en"
"http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
  <title></title>
  <link href="swcss.css" rel="stylesheet" type="text/css" />
  <link href="tablezb.css" rel="stylesheet" type="text/css" />
  <script src="js-datagrid/scripts/pagerview.js" type="text/javascript"></script>
  <link href="js-datagrid/style.css" rel="stylesheet" type="text/css" />
  <script src="jquery-1.4.1.min.js" type="text/javascript"></script>
  <script type="text/javascript">
    var currentpage = 1;
    $(document).ready(function() {
      $.get("handler.ashx?flag=data&index=" + currentpage,
        function(data) {
          $("#sjxs").append(data);
        });
      $.get("handler.ashx?flag=count",
        function(count) {
          var pager = new pagerview('pager');
          pager.itemcount = count;
          pager.size = 5;
          pager.onclick = function(index) {
            currentpage = index;
            $.get("handler.ashx?flag=data&index=" + index,
              function(data) {
                $("#sjxs tr").each(function(m) {
                  $("#add" + m).remove();
                });
                $("#sjxs").append(data);
              });
          };
          pager.render();
        }
    );
    });
  </script>
</head>
<body>
  <form id="form1" runat="server">
  <fieldset style="width: 98%; border: solid 1px #edf7dc;" class="fileset_style">
  <legend class="leg_style">查询结果</legend>
  <%--<div style="overflow:scroll;width:100%;height:100%">--%>
  <table border="1" cellpadding="0" cellspacing="0" width="100%" align="center" id="sjxs" class="tablestylezb">
  <tr class="trtitle">
  <td colspan="5">当前发票库存情况</td>
  <td colspan="4">购买后一年(含)以上未验旧使用情况</td>
  <td></td>
  </tr>
  <tr class="trtitle">
  <td>管理机关</td>
  <td>合计</td>
  <td>机打发票</td>
  <td>手工发票</td>
  <td>冠名发票</td>
  <td>其他</td>
  <td>正常户</td>
  <td>占库存户数的比率</td>
  <td>非正常户</td>
  <td>证件失效户</td>
  </tr>
  <tr class="trtitle">
  <td>0</td>
  <td>1</td>
  <td>2</td>
  <td>3</td>
  <td>4</td>
  <td>5</td>
  <td>6</td>
  <td>7=6/1</td>
  <td>8</td>
  <td>9</td>
  </tr>
  </table>
  <%--</div>--%>
  </fieldset>
   <div id="pager"></div>
   <div id="info"></div>
  </form>
</body>
</html>

处理页面程序如下:

<%@ webhandler language="c#" class="handler" %>
using system;
using system.web;
using system.data;
using system.data.sqlclient;
using system.text;
public class handler : ihttphandler {
  public void processrequest (httpcontext context) {
    context.response.contenttype = "text/plain";
    if (context.request.querystring["flag"].equals("data"))
    {
      context.response.write(getresult(5,int32.parse(context.request.querystring["index"])));
    }
    if (context.request.querystring["flag"].equals("count"))
    {
      context.response.write(getcount());
    }
  }
  private string getresult()
  {
    datatable dt = new datatable();
    using (sqlconnection conn = new sqlconnection(system.configuration.configurationmanager.appsettings["conn"]))
    {
      conn.open();
      string sql = "select * from testblog ";
      sqldataadapter sda = new sqldataadapter(sql, conn);
      sda.fill(dt);
    }
    int rlen = dt.rows.count;
    int clen = dt.columns.count;
    stringbuilder sb = new stringbuilder();
    for (int j = 0; j < rlen; j++)
    {
      sb.append("<tr>");
      for (int i = 0; i < clen; i++)
      {
        sb.append("<td>");
        sb.append(dt.rows[j][i].tostring());
        sb.append("</td>");
      }
      sb.append("</tr>");
    }
    return sb.tostring();
  }
  private string getresult(int pagecount,int currentpage)
  {
    datatable dt = new datatable();
    using (sqlconnection conn = new sqlconnection(system.configuration.configurationmanager.appsettings["conn"]))
    {
      conn.open();
      string sql = "select top "+pagecount+" * from testblog tb where ydid not in (select top "+pagecount*(currentpage-1)+" ydid from testblog tb2) ";
      sqldataadapter sda = new sqldataadapter(sql,conn);
      sda.fill(dt);
    }
    int rlen = dt.rows.count;
    int clen = dt.columns.count;
    stringbuilder sb = new stringbuilder();
    for (int j = 0; j < rlen; j++)
    {
      sb.append("<tr id=add"+j+">");
      for (int i = 0; i < clen; i++)
      {
        sb.append("<td>");
        sb.append(dt.rows[j][i].tostring());
        sb.append("</td>");
      }
      sb.append("</tr>");
    }
    return sb.tostring();
  }
  private string getcount()
  {
    datatable dt = new datatable();
    using (sqlconnection conn = new sqlconnection(system.configuration.configurationmanager.appsettings["conn"]))
    {
      conn.open();
      string sql = "select count(*) from testblog";
      sqldataadapter sda = new sqldataadapter(sql, conn);
      sda.fill(dt);
    }
    return dt.rows[0][0].tostring();
  }
  public bool isreusable {
    get {
      return false;
    }
  }
}

可惜的是,在ie7中显示不出来结果,但是在chrome、firefox和opera中倒是显示都正常,感兴趣的朋友可以对此加以完善。

希望本文所述对大家asp.net程序设计有所帮助。