[学习公共库]Datalist分页代码 |
麦林 发表于 2007/7/18 16:20:28 | 界面源代码:<body> <form id="form1" runat="server"> <asp:DataList ID="DataList" runat="server" CellPadding="4" ForeColor="#333333" Width="663px"> <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" /> <SelectedItemStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" /> <AlternatingItemStyle BackColor="White" ForeColor="#284775" /> <ItemStyle BackColor="#F7F6F3" ForeColor="#333333" /> <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" /> <ItemTemplate> 类名称:<%# DataBinder.Eval(Container.DataItem,"mastername") %> </ItemTemplate> </asp:DataList><br /> 共有 <asp:Label ID="lblRecordCount" runat="server" ForeColor="Red" Width="39px"></asp:Label>条记录,当前为<asp:Label id="lblCurrentPage" ForeColor="red" runat="server" />/<asp:Label id="lblPageCount" ForeColor="red" runat="server" />页 <asp:Button ID="lbnPrevPage" runat="server" OnClick="lbnPrevPage_Click" Text="上一页" /> <asp:Button ID="lbnNextPage" runat="server" OnClick="lbnNextPage_Click" Text="下一页" /> </form></body>
实现代码:public partial class _Default : System.Web.UI.Page { SqlConnection conn; int pageSize,RecordCount,PageCount,CurrentPage;
protected void Page_Load(object sender, EventArgs e) { pageSize = 8; string MyConnString = "server= .;database=webSite;uid = sa;pwd = 123"; conn = new SqlConnection(MyConnString); conn.Open();
if(!IsPostBack) { ListBind(); CurrentPage = 0; ViewState["PageIndex"] = 0;
RecordCount = CalculateRecord(); lblRecordCount.Text = RecordCount.ToString();
PageCount = (RecordCount%pageSize>0)?(RecordCount/pageSize)+1:(RecordCount/pageSize); lblPageCount.Text = PageCount.ToString(); ViewState["PageCount"] = PageCount; } } public int CalculateRecord() { int intCount; string strCount = "select count(*) as co from master"; SqlCommand cmd = new SqlCommand(strCount,conn); SqlDataReader dr = cmd.ExecuteReader(); //SqlDataReader dr = cmd.EndExecuteReader(); if (dr.Read()) { intCount = Int32.Parse(dr["co"].ToString()); } else { intCount = 0; } dr.Close(); return intCount; }
public void ListBind() { int StartIndex; StartIndex = CurrentPage * pageSize; string strSel = "select * from master"; DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter(strSel, conn); da.Fill(ds, StartIndex, pageSize, "ok");
DataList.DataSource = ds.Tables["ok"].DefaultView; DataList.DataBind();
lbnNextPage.Enabled = true; lbnPrevPage.Enabled = true;
if (CurrentPage == (PageCount - 1)) lbnNextPage.Enabled = false; if (CurrentPage == 0) lbnPrevPage.Enabled = false; lblCurrentPage.Text = (CurrentPage + 1).ToString(); }
protected void lbnNextPage_Click(object sender, EventArgs e) { CurrentPage = (int)ViewState["PageIndex"]; PageCount = (int)ViewState["PageCount"]; if (CurrentPage < (PageCount - 1)) CurrentPage++; ViewState["PageIndex"] = CurrentPage; ListBind();
} protected void lbnPrevPage_Click(object sender, EventArgs e) { CurrentPage = (int)ViewState["PageIndex"]; PageCount = (int)ViewState["PageCount"]; if (CurrentPage > 0) CurrentPage--; ViewState["PageIndex"] = CurrentPage; ListBind(); }}
|
|
|

.: 公告
|
« | September 2025 | » | 日 | 一 | 二 | 三 | 四 | 五 | 六 | | 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 | | | | | |
|
.: 我的分类(专题)
|

.: 最新日志
.: 最新回复
|

blog名称:栗色?蓝色? 日志总数:449 评论数量:201 留言数量:37 访问次数:2260039 建立时间:2006年5月16日 |
|

.: 留言板
|

.: 链接
|

|