一句话轻松搞定asp.net分页

时间:2022-09-19 14:07:31

rows 是客户端传过来的行数,page是页码,传参就需要就两个参数就行,sql语句中_row 和_page 自己声明的局部变量,值还是相应的row 和page ,为了运算而已。

用数据库类获得它的DataTable,转换为json格式通过一般处理程序传到客户端,客户端显示就ok了。这里我使用的是easyui datagrid进行接收和传参。这是大体的思路。

string sql = "select top " + rows + " * from TestData where testID not in(select top " + (_rows * (_page - 1)) + " testID from TestData order by testID desc) order by testID desc";

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
//DataTable 转换成json,这里带了“total”,传给客户端的数据总数,不传这个,客户端不会显示总数据是多少条多少条的。
public static string CreateJsonParameters(DataTable dt, bool displayCount, int totalcount)
    {
      StringBuilder JsonString = new StringBuilder();
      //Exception Handling   
      if (dt != null)
      {
        JsonString.Append("{ ");
        if (displayCount)
        {
          JsonString.Append("\"total\":");
          JsonString.Append(totalcount);
          JsonString.Append(",");
        }
        JsonString.Append("\"rows\":[ ");
        for (int i = 0; i < dt.Rows.Count; i++)
        {
          JsonString.Append("{ ");
          for (int j = 0; j < dt.Columns.Count; j++)
          {
            if (j < dt.Columns.Count - 1)
            {
              //if (dt.Rows[i][j] == DBNull.Value) continue;
              if (dt.Columns[j].DataType == typeof(bool))
              {
                JsonString.Append("\"" + dt.Columns[j].ColumnName + "\":" +
                         dt.Rows[i][j].ToString().ToLower() + ",");
              }
              else if (dt.Columns[j].DataType == typeof(string))
              {
                JsonString.Append("\"" + dt.Columns[j].ColumnName + "\":" + "\"" +
                         dt.Rows[i][j].ToString().Replace("\"", "\\\"") + "\",");
              }
              else
              {
                JsonString.Append("\"" + dt.Columns[j].ColumnName + "\":" + "\"" + dt.Rows[i][j] + "\",");
              }
            }
            else if (j == dt.Columns.Count - 1)
            {
              //if (dt.Rows[i][j] == DBNull.Value) continue;
              if (dt.Columns[j].DataType == typeof(bool))
              {
                JsonString.Append("\"" + dt.Columns[j].ColumnName + "\":" +
                         dt.Rows[i][j].ToString());
              }
              else if (dt.Columns[j].DataType == typeof(string))
              {
                JsonString.Append("\"" + dt.Columns[j].ColumnName + "\":" + "\"" +
                         dt.Rows[i][j].ToString().Replace("\"", "\\\"") + "\"");
              }
              else
              {
                JsonString.Append("\"" + dt.Columns[j].ColumnName+ "\":" + "\"" + dt.Rows[i][j] + "\"");
              }
            }
          }
          /*end Of String*/
          if (i == dt.Rows.Count - 1)
          {
            JsonString.Append("} ");
          }
          else
          {
            JsonString.Append("}, ");
          }
        }
        JsonString.Append("]");
        JsonString.Append("}");
        return JsonString.ToString().Replace("\n", "");
      }
      else
      {
        return null;
      }
    }

以上就是本文的全部内容,希望对大家的学习有所帮助。