自定义翻页的例子
1
using System;
2
using System.Collections;
3
using System.ComponentModel;
4
using System.Data;
5
using System.Drawing;
6
using System.Web;
7
using System.Web.SessionState;
8
using System.Web.UI;
9
using System.Web.UI.WebControls;
10
using System.Web.UI.HtmlControls;
11
using System.Data.SqlClient;
12
using System.Configuration;
13
using System.Web.Configuration;
14
using System.IO;
15
using System.Text;
16![]()
17![]()
18
public partial class _Default : System.Web.UI.Page
19
{
20![]()
21
//private static string connString = System.ConfigurationSettings.AppSettings["ConnString"];//VS2003的用法
22
private static string connString = System.Configuration.ConfigurationManager.AppSettings["ConnString"];//获取数据库连接
23
private static string strSql = System.Configuration.ConfigurationManager.AppSettings["ConnStringSql"];//获取SQL执行语句
24
private int recordCount;
25
private int pageCount;
26![]()
27
protected void Page_Load(object sender, System.EventArgs e)
28
{
29
// 在此处放置用户代码以初始化页面
30
this.Label1.Visible = false;
31
if (!Page.IsPostBack)
32
{
33
this.LBtnFirst.Enabled = false;
34
this.LBtnPrev.Enabled = false;
35
this.LBtnNext.Enabled = false;
36
this.LBtnLast.Enabled = false;
37
DataGridDataBind();
38
}
39
}
40
//绑定数据
41
private void DataGridDataBind()
42
{
43
DataSet ds = GetCustomersData();
44
recordCount = ds.Tables[0].Rows.Count;
45![]()
46
//获取当前的页数
47
pageCount = (int)Math.Ceiling(recordCount * 1.0 / PageSize);
48
//避免纪录从有到无时,并且已经进行过反页的情况下CurrentPageIndex > PageCount出错
49
if (recordCount == 0)
50
{
51
this.DataGrid1.CurrentPageIndex = 0;
52
}
53
else if (this.DataGrid1.CurrentPageIndex >= pageCount)
54
{
55
this.DataGrid1.CurrentPageIndex = pageCount - 1;
56
}
57![]()
58
this.DataGrid1.DataSource = ds;
59
this.DataGrid1.DataBind();
60
NavigationStateChange();
61
}
62![]()
63
Web 窗体设计器生成的代码
82
protected void LBtnNavigation_Click(object sender, System.EventArgs e)
83
{
84
LinkButton btn = (LinkButton)sender;
85
switch (btn.CommandName)
86
{
87
case "First":
88
PageIndex = 0;
89
break;
90
case "Prev"://if( PageIndex > 0 )
91
PageIndex = PageIndex - 1;
92
break;
93
case "Next"://if( PageIndex < PageCount -1)
94
PageIndex = PageIndex + 1;
95
break;
96
case "Last":
97
PageIndex = PageCount - 1;
98
break;
99
}
100
DataGridDataBind();
101
}
102
public static DataSet GetCustomersData()
103
{
104
SqlConnection conn = new SqlConnection(connString);//执行SQL
105
SqlCommand comm = new SqlCommand(strSql, conn);
106
SqlDataAdapter dataAdapter = new SqlDataAdapter(comm);
107![]()
108
DataSet ds = new DataSet();
109
dataAdapter.Fill(ds);
110
return ds;
111
}
112![]()
113
/// <summary>
114
/// 控制导航按钮或数字的状态
115
/// </summary>
116
public void NavigationStateChange()
117
{
118![]()
119
if (PageCount <= 1)//( RecordCount <= PageSize )//小于等于一页
120
{
121![]()
122
this.LBtnFirst.Enabled = false;
123
this.LBtnPrev.Enabled = false;
124
this.LBtnNext.Enabled = false;
125
this.LBtnLast.Enabled = false;
126![]()
127
}
128
else //有多页
129
{
130![]()
131
if (PageIndex == 0)//当前为第一页
132
{
133
this.LBtnFirst.Enabled = false;
134
this.LBtnPrev.Enabled = false;
135
this.LBtnNext.Enabled = true;
136
this.LBtnLast.Enabled = true;
137
}
138
else if (PageIndex == PageCount - 1)//当前为最后页
139
{
140
this.LBtnFirst.Enabled = true;
141
this.LBtnPrev.Enabled = true;
142
this.LBtnNext.Enabled = false;
143
this.LBtnLast.Enabled = false;
144
}
145
else //中间页
146
{
147
this.LBtnFirst.Enabled = true;
148
this.LBtnPrev.Enabled = true;
149
this.LBtnNext.Enabled = true;
150
this.LBtnLast.Enabled = true;
151
}
152
}
153![]()
154
if (RecordCount == 0)//当没有纪录时DataGrid.PageCount会显示1页
155![]()
156
this.LtlPageCount.Text = "0";
157![]()
158
else
159![]()
160
this.LtlPageCount.Text = PageCount.ToString();
161![]()
162
if (RecordCount == 0)
163![]()
164
this.LtlPageIndex.Text = "0";
165![]()
166
else
167![]()
168
this.LtlPageIndex.Text = (PageIndex + 1).ToString();//在有页数的情况下前台显示页数加1
169
this.LtlPageSize.Text = PageSize.ToString();
170
this.LtlRecordCount.Text = RecordCount.ToString();
171
}
172![]()
173
protected void Button1_Click(object sender, System.EventArgs e)
174
{
175
if (TextBox1.Text == string.Empty)
176
{
177
this.Label1.Visible = true;
178
}
179
else
180
{
181![]()
182
this.Label1.Visible = false;
183
this.DataGrid1.CurrentPageIndex = 0;
184
//DropDownList1.ClearSelection();
185
string sDropDownList = DropDownList1.SelectedItem.Value;
186
string sTextBox = TextBox1.Text;
187
string sWhere = "";
188
switch (sDropDownList)
189
{
190
case "0":
191
sWhere = " replace(Col002,' ','') like '%" + sTextBox + "%' order by Col007";
192
break;
193
case "1":
194
sWhere = " replace(Col004,' ','') like '%" + sTextBox + "%' order by Col007";
195
break;
196
case "2":
197
sWhere = " replace(Col005,' ','') like '%" + sTextBox + "%' order by Col007";
198
break;
199
}
200![]()
201
strSql = System.Configuration.ConfigurationManager.AppSettings["ConnStringSql"];
202
strSql = strSql + "where " + sWhere;
203![]()
204
try
205
{
206
SqlConnection conn = new SqlConnection(connString);//执行SQL
207
SqlCommand comm = new SqlCommand(strSql, conn);
208
SqlDataAdapter dataAdapter = new SqlDataAdapter(comm);
209![]()
210
DataSet ds = new DataSet();
211
dataAdapter.Fill(ds);
212
DataGridDataBind();
213![]()
214
}
215
catch (Exception ex)
216
{
217
Response.Write("数据库读写出错");
218
Response.Write(ex.Message);
219
Response.End();
220
}
221
}
222
}
223![]()
224
public int PageCount
225
{
226
get { return this.DataGrid1.PageCount; }
227
}
228![]()
229
//页大小
230![]()
231
public int PageSize
232
{
233
get { return this.DataGrid1.PageSize; }
234
}
235![]()
236
//页索引,从零开始
237
public int PageIndex
238
{
239
get { return this.DataGrid1.CurrentPageIndex; }
240
set { this.DataGrid1.CurrentPageIndex = value; }
241
}
242
// 纪录总数
243
public int RecordCount
244
{
245
get { return recordCount; }
246
set { recordCount = value; }
247
}
248
}
249![]()
using System;2
using System.Collections;3
using System.ComponentModel;4
using System.Data;5
using System.Drawing;6
using System.Web;7
using System.Web.SessionState;8
using System.Web.UI;9
using System.Web.UI.WebControls;10
using System.Web.UI.HtmlControls;11
using System.Data.SqlClient;12
using System.Configuration;13
using System.Web.Configuration;14
using System.IO;15
using System.Text;16

17

18
public partial class _Default : System.Web.UI.Page19
{20

21
//private static string connString = System.ConfigurationSettings.AppSettings["ConnString"];//VS2003的用法22
private static string connString = System.Configuration.ConfigurationManager.AppSettings["ConnString"];//获取数据库连接23
private static string strSql = System.Configuration.ConfigurationManager.AppSettings["ConnStringSql"];//获取SQL执行语句24
private int recordCount;25
private int pageCount;26

27
protected void Page_Load(object sender, System.EventArgs e)28
{29
// 在此处放置用户代码以初始化页面30
this.Label1.Visible = false;31
if (!Page.IsPostBack)32
{33
this.LBtnFirst.Enabled = false;34
this.LBtnPrev.Enabled = false;35
this.LBtnNext.Enabled = false;36
this.LBtnLast.Enabled = false;37
DataGridDataBind();38
}39
}40
//绑定数据41
private void DataGridDataBind()42
{43
DataSet ds = GetCustomersData();44
recordCount = ds.Tables[0].Rows.Count;45

46
//获取当前的页数47
pageCount = (int)Math.Ceiling(recordCount * 1.0 / PageSize);48
//避免纪录从有到无时,并且已经进行过反页的情况下CurrentPageIndex > PageCount出错49
if (recordCount == 0)50
{51
this.DataGrid1.CurrentPageIndex = 0;52
}53
else if (this.DataGrid1.CurrentPageIndex >= pageCount)54
{55
this.DataGrid1.CurrentPageIndex = pageCount - 1;56
}57

58
this.DataGrid1.DataSource = ds;59
this.DataGrid1.DataBind();60
NavigationStateChange();61
}62

63
Web 窗体设计器生成的代码82
protected void LBtnNavigation_Click(object sender, System.EventArgs e)83
{84
LinkButton btn = (LinkButton)sender;85
switch (btn.CommandName)86
{87
case "First":88
PageIndex = 0;89
break;90
case "Prev"://if( PageIndex > 0 )91
PageIndex = PageIndex - 1;92
break;93
case "Next"://if( PageIndex < PageCount -1)94
PageIndex = PageIndex + 1;95
break;96
case "Last":97
PageIndex = PageCount - 1;98
break;99
}100
DataGridDataBind();101
}102
public static DataSet GetCustomersData()103
{104
SqlConnection conn = new SqlConnection(connString);//执行SQL105
SqlCommand comm = new SqlCommand(strSql, conn);106
SqlDataAdapter dataAdapter = new SqlDataAdapter(comm);107

108
DataSet ds = new DataSet();109
dataAdapter.Fill(ds);110
return ds;111
}112

113
/// <summary>114
/// 控制导航按钮或数字的状态115
/// </summary>116
public void NavigationStateChange()117
{118

119
if (PageCount <= 1)//( RecordCount <= PageSize )//小于等于一页120
{121

122
this.LBtnFirst.Enabled = false;123
this.LBtnPrev.Enabled = false;124
this.LBtnNext.Enabled = false;125
this.LBtnLast.Enabled = false;126

127
}128
else //有多页129
{130

131
if (PageIndex == 0)//当前为第一页132
{133
this.LBtnFirst.Enabled = false;134
this.LBtnPrev.Enabled = false;135
this.LBtnNext.Enabled = true;136
this.LBtnLast.Enabled = true;137
}138
else if (PageIndex == PageCount - 1)//当前为最后页 139
{140
this.LBtnFirst.Enabled = true;141
this.LBtnPrev.Enabled = true;142
this.LBtnNext.Enabled = false;143
this.LBtnLast.Enabled = false;144
}145
else //中间页146
{147
this.LBtnFirst.Enabled = true;148
this.LBtnPrev.Enabled = true;149
this.LBtnNext.Enabled = true;150
this.LBtnLast.Enabled = true;151
}152
}153

154
if (RecordCount == 0)//当没有纪录时DataGrid.PageCount会显示1页155

156
this.LtlPageCount.Text = "0";157

158
else159

160
this.LtlPageCount.Text = PageCount.ToString();161

162
if (RecordCount == 0)163

164
this.LtlPageIndex.Text = "0";165

166
else167

168
this.LtlPageIndex.Text = (PageIndex + 1).ToString();//在有页数的情况下前台显示页数加1169
this.LtlPageSize.Text = PageSize.ToString();170
this.LtlRecordCount.Text = RecordCount.ToString();171
}172

173
protected void Button1_Click(object sender, System.EventArgs e)174
{175
if (TextBox1.Text == string.Empty)176
{177
this.Label1.Visible = true;178
}179
else180
{181

182
this.Label1.Visible = false;183
this.DataGrid1.CurrentPageIndex = 0;184
//DropDownList1.ClearSelection();185
string sDropDownList = DropDownList1.SelectedItem.Value;186
string sTextBox = TextBox1.Text;187
string sWhere = "";188
switch (sDropDownList)189
{190
case "0":191
sWhere = " replace(Col002,' ','') like '%" + sTextBox + "%' order by Col007";192
break;193
case "1":194
sWhere = " replace(Col004,' ','') like '%" + sTextBox + "%' order by Col007";195
break;196
case "2":197
sWhere = " replace(Col005,' ','') like '%" + sTextBox + "%' order by Col007";198
break;199
}200

201
strSql = System.Configuration.ConfigurationManager.AppSettings["ConnStringSql"];202
strSql = strSql + "where " + sWhere;203

204
try205
{206
SqlConnection conn = new SqlConnection(connString);//执行SQL207
SqlCommand comm = new SqlCommand(strSql, conn);208
SqlDataAdapter dataAdapter = new SqlDataAdapter(comm);209

210
DataSet ds = new DataSet();211
dataAdapter.Fill(ds);212
DataGridDataBind();213

214
}215
catch (Exception ex)216
{217
Response.Write("数据库读写出错");218
Response.Write(ex.Message);219
Response.End();220
}221
}222
}223

224
public int PageCount225
{226
get { return this.DataGrid1.PageCount; }227
}228

229
//页大小230

231
public int PageSize232
{233
get { return this.DataGrid1.PageSize; }234
}235

236
//页索引,从零开始237
public int PageIndex238
{239
get { return this.DataGrid1.CurrentPageIndex; }240
set { this.DataGrid1.CurrentPageIndex = value; }241
}242
// 纪录总数243
public int RecordCount244
{245
get { return recordCount; }246
set { recordCount = value; }247
}248
}249



浙公网安备 33010602011771号