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

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

asp.net异步获取datatable并显示的实现方法_第1张图片

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

<%@ Page Language="C#" AutoEventWireup="true"
CodeFile="MethodOne.aspx.cs" Inherits="_Default" %>



  
  
  
  
  
  
  


  
查询结果 <%--
--%>
当前发票库存情况 购买后一年(含)以上未验旧使用情况
管理机关 合计 机打发票 手工发票 冠名发票 其他 正常户 占库存户数的比率 非正常户 证件失效户
0 1 2 3 4 5 6 7=6/1 8 9
<%--
--%>

处理页面程序如下:

<%@ 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("");
      for (int i = 0; i < cLen; i++)
      {
        sb.Append("");
        sb.Append(dt.Rows[j][i].ToString());
        sb.Append("");
      }
      sb.Append("");
    }
    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("");
      for (int i = 0; i < cLen; i++)
      {
        sb.Append("");
        sb.Append(dt.Rows[j][i].ToString());
        sb.Append("");
      }
      sb.Append("");
    }
    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程序设计有所帮助。

你可能感兴趣的:(asp.net异步获取datatable并显示的实现方法)