实现高效分页的方法
1
<script runat=server>
2
SqlConnection conNorthwind;
3
string strSelect;
4
int intStartIndex;
5
int intEndIndex;
6
7
void Page_Load(Object sender , EventArgs e)
8
{
9
SqlCommand cmdSelect;
10
11
conNorthwind = new SqlConnection( @"Server=localhost;Integrated Security=SSPI;Database=Northwind" );
12
13
if (! IsPostBack )
14
{
15
// Get Total Pages
16
strSelect = "Select Count(*) From Products";
17
cmdSelect = new SqlCommand( strSelect, conNorthwind );
18
conNorthwind.Open();
19
dgrdProducts.VirtualItemCount = ( (int)cmdSelect.ExecuteScalar() / dgrdProducts.PageSize );
20
conNorthwind.Close();
21
BindDataGrid();
22
}
23
}
24
25
void BindDataGrid ()
26
{
27
SqlDataAdapter dadProducts;
28
DataSet dstProducts;
29
30
intEndIndex = intStartIndex + dgrdProducts.PageSize;
31
strSelect = "Select * From Products Where ProductID > @startIndex And ProductID <= @endIndex Order By ProductID";
32
dadProducts = new SqlDataAdapter( strSelect, conNorthwind );
33
dadProducts.SelectCommand.Parameters.Add( "@startIndex", intStartIndex );
34
dadProducts.SelectCommand.Parameters.Add( "@endIndex", intEndIndex );
35
dstProducts = new DataSet();
36
dadProducts.Fill( dstProducts );
37
38
dgrdProducts.DataSource = dstProducts;
39
dgrdProducts.DataBind();
40
}
41
42
void dgrdProducts_PageIndexChanged( object s, DataGridPageChangedEventArgs e ) {
43
intStartIndex = ( e.NewPageIndex * dgrdProducts.PageSize );
44
dgrdProducts.CurrentPageIndex = e.NewPageIndex;
45
BindDataGrid();
46
}
47
48
</Script>
49
50
<html>
51
<head><title>DataGridCustomPaging.aspx</title></head>
52
<body>
53
<form Runat="Server">
54
55
<asp:DataGrid
56
ID="dgrdProducts"
57
AllowPaging="True"
58
AllowCustomPaging="True"
59
PageSize="3"
60
OnPageIndexChanged="dgrdProducts_PageIndexChanged"
61
PagerStyle-Mode="NumericPages"
62
CellPadding="3"
63
Runat="Server" />
64
65
</form>
66
</body>
67
</html>
68
<script runat=server>2
SqlConnection conNorthwind;3
string strSelect;4
int intStartIndex;5
int intEndIndex;6

7
void Page_Load(Object sender , EventArgs e) 8
{9
SqlCommand cmdSelect;10

11
conNorthwind = new SqlConnection( @"Server=localhost;Integrated Security=SSPI;Database=Northwind" );12
13
if (! IsPostBack ) 14
{15
// Get Total Pages16
strSelect = "Select Count(*) From Products";17
cmdSelect = new SqlCommand( strSelect, conNorthwind );18
conNorthwind.Open();19
dgrdProducts.VirtualItemCount = ( (int)cmdSelect.ExecuteScalar() / dgrdProducts.PageSize );20
conNorthwind.Close();21
BindDataGrid();22
}23
}24

25
void BindDataGrid () 26
{27
SqlDataAdapter dadProducts;28
DataSet dstProducts;29

30
intEndIndex = intStartIndex + dgrdProducts.PageSize;31
strSelect = "Select * From Products Where ProductID > @startIndex And ProductID <= @endIndex Order By ProductID";32
dadProducts = new SqlDataAdapter( strSelect, conNorthwind );33
dadProducts.SelectCommand.Parameters.Add( "@startIndex", intStartIndex );34
dadProducts.SelectCommand.Parameters.Add( "@endIndex", intEndIndex );35
dstProducts = new DataSet();36
dadProducts.Fill( dstProducts );37

38
dgrdProducts.DataSource = dstProducts;39
dgrdProducts.DataBind();40
}41

42
void dgrdProducts_PageIndexChanged( object s, DataGridPageChangedEventArgs e ) {43
intStartIndex = ( e.NewPageIndex * dgrdProducts.PageSize );44
dgrdProducts.CurrentPageIndex = e.NewPageIndex;45
BindDataGrid();46
}47

48
</Script>49

50
<html>51
<head><title>DataGridCustomPaging.aspx</title></head>52
<body>53
<form Runat="Server">54

55
<asp:DataGrid56
ID="dgrdProducts"57
AllowPaging="True"58
AllowCustomPaging="True"59
PageSize="3"60
OnPageIndexChanged="dgrdProducts_PageIndexChanged"61
PagerStyle-Mode="NumericPages"62
CellPadding="3"63
Runat="Server" />64

65
</form>66
</body>67
</html>68

使用条件:要有一个主键列(ProductID)。


浙公网安备 33010602011771号