分页效果如下:
后台代码:code C#
一 、申明全局变量:
public static int startIndex =0; //起始页
public static int pagesize =8; //每页记录数
public static int pagecount =1; //总记录
public static int countpage = 0; //总页数
public static int nowpage = 1; //当前页
public static DataSet DS = new DataSet(); //接收数据
public static string connstring="Data Source=.//SQLEXPRESS;AttachDbFilename=|DataDirectory|//payforboos.mdf;Integrated Security=True;User Instance=True"; //连接字符串
static SqlConnection nwindConn = new SqlConnection(connstring);
public static SqlDataAdapter adapter = new SqlDataAdapter("", nwindConn);
static SqlCommand selCmd = adapter.SelectCommand; //为查询适配器创建查询语句
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
selCmd.CommandText = SqlDataSource1.SelectCommand.ToString();
getdatatable();
DataList1.DataSource = DS.Tables["allbooks"].DefaultView;
DataList1.DataBind();
DataTable dt = new DataTable();
adapter.Fill(dt);
pagecount = dt.Rows.Count;
ViewState["pagecount"] = pagecount;
countpage = (int)Math.Ceiling((double) pagecount / pagesize); //计算页数 向上取整
}
}
static public void getdatatable() //自定义函数
{
nwindConn.Close();
nwindConn.Open();
DS.Clear(); //将dataset中的数据清空 否则会形成累计
selCmd.Parameters.Clear();
adapter.Fill(DS, startIndex, pagesize, "allbooks"); //从第startindex条数据开始 读取pagesize条数据 添加到DS中
nwindConn.Close();
}
protected void Button3_Click(object sender, EventArgs e) //下一页
{
if (nowpage <countpage )
{
startIndex = startIndex + pagesize;
nowpage++;
}
selCmd.CommandText = SqlDataSource1.SelectCommand.ToString();
getdatatable();
DataList1.DataSource = DS.Tables["allbooks"];
DataList1.DataBind();
}
protected void Button2_Click(object sender, EventArgs e) //上一页
{
if (nowpage > 1 )
{
startIndex -= pagesize;
nowpage--;
}
selCmd.CommandText = SqlDataSource1.SelectCommand.ToString();
getdatatable();
DataList1.DataSource = DS.Tables["allbooks"];
DataList1.DataBind();
}
protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e) //添加页码
{
foreach (Control c in e.Item.Controls) //查询在footerTemplate
{
DropDownList dplist = c as DropDownList;
if (dplist != null)
{
for (int i = 1; i <=countpage ; i++)
{
dplist.Items.Add(i.ToString());
}
}
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e) //查找第I 页
{
DropDownList dplist = (DropDownList)sender; //获取sender对象中的dropdownlist的值
int selectpage = int.Parse(dplist.SelectedValue.ToString());
nowpage = selectpage;
startIndex = (nowpage - 1) * pagesize;
getdatatable();
DataList1.DataSource = DS.Tables["allbooks"];
DataList1.DataBind();
}
前台代码:
<FooterTemplate>
<table class="footstyle">
<tr>
<td>
总共<b> <%=countpage %> </b>页/当前第<b> <%=nowpage %> </b>页</td>
<td>
总计<b> <%=pagecount %> </b>条记录</td>
<td align="right">
<asp:Button ID="Button2" runat="server" οnclick="Button2_Click" Text="上一页" />
<asp:DropDownList ID="DropDownList1" runat="server" Height="19px" Width="58px"
AutoPostBack="True"
onselectedindexchanged="DropDownList1_SelectedIndexChanged" >
</asp:DropDownList>
<asp:Button ID="Button3" runat="server" οnclick="Button3_Click" Text="下一页" />
</td>
</tr>
</table>
</FooterTemplate>