datalist实现分页显示
<%@ Import NameSpace="System.Data.OleDb"%>
<Script Language="C#" Runat="Server">
OleDbConnection objconn;
int pagecount,pagesize,recordcount,currentpage;
public void Page_Load(Object sender, EventArgs e)
{
pagesize = 5;
string connstr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" +Server.MapPath("price.mdb");
objconn=new OleDbConnection(connstr);
objconn.Open();
if (!Page.IsPostBack)
{
listbind();
currentpage=0;
ViewState["pageindex"] = 0;
//总的记录数
recordcount = calrecordcount();
pgsize.Text = recordcount.ToString();
//总的页数
pagecount = recordcount/pagesize;
pgcount.Text = pagecount.ToString();
ViewState["pagecount"] = pagecount;
}
}
//计算有多少条记录
public int calrecordcount()
{
int intcount;
string strcount="Select count(*) as co From 零部件报价表";
OleDbCommand objcmd = new OleDbCommand(strcount, objconn);
OleDbDataReader dr = objcmd.ExecuteReader();
if (dr.Read())
{
intcount = Int32.Parse(dr["co"].ToString());
}
else
{
intcount = 0;
}
dr.Close();
return intcount;
}
ICollection CreateSource()
{
int starpage;
starpage = currentpage * pagesize;
string strsql = "Select * From 零部件报价表";
OleDbDataAdapter objcmd = new OleDbDataAdapter(strsql, objconn);
DataSet ds = new DataSet();
objcmd.Fill(ds,starpage,pagesize, "零部件报价表");
return ds.Tables[0].DefaultView;
}
public void listbind()
{
dl1.DataSource = CreateSource();
dl1.DataBind();
prev1.Enabled = true;
next1.Enabled = true;
if (currentpage == (pagecount - 1)) next1.Enabled = false;
if (currentpage == 0) prev1.Enabled = false;
pgcurrent.Text = (currentpage+1).ToString();
}
public void click(Object sender, CommandEventArgs e)
{
currentpage = (int)ViewState["pageindex"];
pagecount =(int)ViewState["pagecount"];
string cmd = e.CommandName;
switch (cmd)
{
case "prev":
currentpage --;
break;
case "next":
currentpage ++;
break;
}
ViewState["pageindex"] = currentpage;
listbind();
}
</Script>
<Html>
<Title>datalist实现分页显示!</Title>
<Body>
<Form Runat="server">
<p align="center">共有<asp:Label id="pgsize" runat="server"/>条记录,有<asp:Label id="pgcurrent" runat="server"/>/<asp:Label id="pgcount" runat="server"/>页.<hr>
<asp:DataList runat="server" id="dl1"
HeaderStyle-BackColor="#A33842"
ItemStyle-BackColor="white"
AlternateItemStyle-BackColor="#D6E7DE"
>
<ItemTemplate>
品牌: <%# DataBinder.Eval(Container.DataItem, "品牌")%>
</ItemTemplate>
</asp:DataList>
<asp:LinkButton id="prev1" runat="server" Text ="上一页" CommandName="prev" OnCommand="click"/>
<asp:LinkButton id="next1" runat="server" Text="下一页" CommandName = "next" OnCommand="click"/>
</Form>
</Body>
</Html>