datatlist分页

来源:互联网 发布:地平线 你知我知 编辑:程序博客网 时间:2024/05/20 03:41

 前台代码

 

<%@ Page Language="C#" AutoEventWireup="true" Trace="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    
<title>Untitled Page</title>
</head>
<body>
    
<form id="form1" runat="server">
    
<div>
    
<asp:DataList ID="dlst" runat="server">
    
<HeaderTemplate>姓名</HeaderTemplate>
    
<ItemTemplate><%#DataBinder.Eval(Container.DataItem,"name"%></ItemTemplate>
    
</asp:DataList>
    
<table><tr><td>
    
<asp:LinkButton id="lbnPrevPage" Text="上一页" CommandName="prev" OnCommand="Page_OnClick" runat="server" />
  
<asp:LinkButton id="lbnNextPage" Text="下一页" CommandName="next" OnCommand="Page_OnClick" runat="server" />
  共有
<asp:Label id="lblRecordCount" ForeColor="red" runat="server" />条记录
  当前为
<asp:Label id="lblCurrentPage" ForeColor="red" runat="server" />/<asp:Label id="lblPageCount" ForeColor="red" runat="server" />&nbsp;&nbsp;
  
<asp:dropdownlist ID="Ddl_PageNumber" runat="server" AutoPostBack="true" CssClass="lanyu"></asp:dropdownlist>
    
</td></tr></table>
    
</div>
    
</form>
</body>
</html>

后台代码

 

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.OleDb;

public partial class _Default : System.Web.UI.Page 
{
    OleDbConnection conn;
    
int PageSize, RecordCount, PageCount, CurrentPage, i;
    
protected void Page_Load(object sender, EventArgs e)
    
{
        PageSize 
= 4;               //设定PageSize 
        
//连接语句 
        string strcon = "provider=Microsoft.jet.OLEDB.4.0;data Source=" + Server.MapPath("."+ "/my.mdb;";
        conn 
= new OleDbConnection(strcon);
        
this.Ddl_PageNumber.SelectedIndexChanged+=new EventHandler(Ddl_PageNumber_SelectedIndexChanged);
        
//MyConn.Open();
        if (!IsPostBack)
        
{
            RecordCount 
= GetRecordCount();
            CurrentPage 
= 1;
            PageCount 
= (int)Math.Floor((double)RecordCount / (double)PageSize);
            ViewState[
"RecordCount"= RecordCount;
            ViewState[
"CurrentPage"= CurrentPage <= 0 ? 1 : CurrentPage;
            BindPageLable();
            BindData();
            BindList(PageCount);
        }

    }


    
void  Ddl_PageNumber_SelectedIndexChanged(object sender, EventArgs e)
    
{
        ViewState[
"CurrentPage"= System.Convert.ToInt32(this.Ddl_PageNumber.SelectedValue);
           BindData();
        BindPageLable();
    }


    
private void BindPageLable()
    
{
        
int RecordCount = (int)ViewState["RecordCount"];
        
int CurrentPage = (int)ViewState["CurrentPage"];
        
int PageCount = (int)Math.Floor((double)RecordCount / (double)PageSize);
        
this.lblRecordCount.Text = RecordCount.ToString();
        
this.lblCurrentPage.Text = CurrentPage.ToString();
        
this.lblPageCount.Text = PageCount.ToString();
        
this.lbnPrevPage.Enabled = CurrentPage <= 1 ? false : true;
        
this.lbnNextPage.Enabled = CurrentPage >= PageCount ? false : true;
        
this.Ddl_PageNumber.ClearSelection();
        
this.Ddl_PageNumber.Items.FindByValue(CurrentPage.ToString()).Selected = true;
    }


    
private void BindList(int pageCount)
    
{
        
for (int i = 1; i <= pageCount; i++)
        
{
            
this.Ddl_PageNumber.Items.Add(new ListItem(""+i.ToString()+"",i.ToString()));
        }

    }


    
private int GetRecordCount()
    
{
        OleDbCommand sda 
= new OleDbCommand("select count(*) from person", conn);
        conn.Open();
        
int rec = (int)sda.ExecuteScalar();
        conn.Close();
        
return rec;
    }


    
private void BindData()
    
{
        OleDbDataAdapter sda 
= new OleDbDataAdapter("select * from person", conn);
        DataSet ds 
= new DataSet();
        
int startIndex = (int)ViewState["CurrentPage"* PageSize;
        sda.Fill(ds,startIndex,PageSize,
"person");
        
this.dlst.DataSource = ds;
        
this.dlst.DataBind();
    }

    
protected void Page_OnClick(Object sender, CommandEventArgs e)
    
{
        
int RecordCount=(int)ViewState["RecordCount"];
        
int CurrentPage=(int)ViewState["CurrentPage"];
        
switch (e.CommandName)
        
{
            
case "prev":
                ViewState[
"CurrentPage"= (int)ViewState["CurrentPage"- 1;
                
break;
            
case "next":
                ViewState[
"CurrentPage"= (int)ViewState["CurrentPage"+ 1;
                
break;
            
default:
                
break;
        }

        BindData();
        BindPageLable();
    }


}

也可以仿照petshop4中的customList,实例有http://blog.csdn.net/CreazeStone05/archive/2007/08/17/1748129.aspx
原创粉丝点击