DataList分頁的一般方法

 看了網上一些關於DataList分頁的列子,有很多分頁方法,主要有利用PageDataSource類,存儲過程分頁,還有就是利用DataSet進行分頁,其實是利用SqlDataAdapter的Fill方法,下面我就用DataSet 就行分頁與大家分享(其實也是網上看來的,然後加上自己的理解,多了一點點功能,呵呵)

.cs文件如下:

 

public static string connectionString = System.Configuration.ConfigurationManager.ConnectionStrings["gomayConnectionString"].ConnectionString;
    
int pageSize, pageCount, recordCount, currentPage;
    
protected void Page_Load(object sender, EventArgs e)
    
{
        pageSize 
= 10;
        
if (!IsPostBack)
        
{
            ListBind();
//綁定DataList
            currentPage = 0;//首頁索引爲0
            ViewState["pageIndex"= 0;//保存頁索引
            
//總記錄數
            recordCount = CalculateRecord();
            lblRecordCount.Text 
="總記錄數:"+ recordCount.ToString();
            
//總頁數
            pageCount = recordCount / pageSize;//取整
            if (recordCount % pageSize > 0//總記錄數不是 頁大小的 整數倍
            {
                pageCount 
= pageCount + 1;
            }

            ViewState[
"pageCount"= pageCount; //保存頁數
            lblPageCount.Text = "頁數:" + pageCount.ToString();
            Label2.Text 
= (currentPage + 1+ "/" + pageCount.ToString();


        }

        btnGo.Attributes.Add(
"onclick"" return validate()");
    }

    
//計算記錄總數
    public int  CalculateRecord() 
    
{

        
int intCount;
        
string strCount = "select count(*) as co from quote";
        SqlConnection con 
= new SqlConnection(connectionString);
        SqlCommand cmd 
= new SqlCommand(strCount, con);
        con.Open();
        SqlDataReader dr 
= cmd.ExecuteReader();
        
if (dr.Read())
        
{
            intCount 
= int.Parse(dr["co"].ToString());
        }

        
else
        
{
            intCount 
= 0;

        }

        dr.Close();
        
return intCount;
        con.Close();


    }

    
//創建數據源
    ICollection CreateSource()
    
{
        SqlConnection con
=new SqlConnection(connectionString);
        
int startIndex;
        startIndex 
= currentPage * pageSize; //某頁的第一條記錄索引
        string strSel = "select * from quote";
        DataSet ds 
= new DataSet();
        SqlDataAdapter da
=new SqlDataAdapter(strSel,con);
        
        da.Fill(ds, startIndex, pageSize, 
"quote");//取一張頁面的記錄數
        return ds.Tables["quote"].DefaultView;
    }

    
public void ListBind()
    
{

        DataList1.DataSource 
= CreateSource();
        DataList1.DataBind();
        lbnNextPage.Enabled 
= true;
        lbnPrevPage.Enabled 
= true;
        
if (currentPage == pageCount - 1)
        
{
            lbnNextPage.Enabled 
= false;
        }

        
if (currentPage == 0)
        
{
            lbnPrevPage.Enabled 
= false;
        }


    }

    
//上一頁 下一頁
    protected void Page_OnClick(object sender, CommandEventArgs e)
    
{
        currentPage 
= (int)ViewState["pageIndex"];
        pageCount 
= (int)ViewState["pageCount"];
        
string cmd = e.CommandName;
        
switch (cmd)
        

            
case "Next":
                
if (currentPage < pageCount - 1) currentPage++;
                
//Label2.Text = (currentPage+1) + "/" + pageCount.ToString();
                break;
            
case "Prev":
                
if (currentPage > 0) currentPage--;
                
//Label2.Text = (currentPage+1) + "/" + pageCount.ToString();
                break;
            
case "First":
                currentPage 
= 0;
                
break;
            
case"Last":
                currentPage 
= pageCount - 1;
                
break;


        }

        Label2.Text 
= (currentPage + 1+ "/" + pageCount.ToString();

        ViewState[
"pageIndex"= currentPage;
        ListBind();

    }

    
//跳轉頁
    protected void btnGo_Click(object sender, EventArgs e)
    
{
        
int intPage=Convert.ToInt32(txtPage.Text);
        
//currentPage = intPage-1;
        pageCount = (int)ViewState["pageCount"];
        
if (intPage > pageCount) //如果輸入的頁數大於總頁數
        {
            currentPage 
= pageCount - 1;
        }

        
else if (intPage <=0)
        
{
            currentPage 
= 0;
        }

        
else
        
{
            currentPage 
= intPage - 1;
        }


        Label2.Text 
= (currentPage + 1+ "/" + pageCount.ToString();
      
        ListBind();
        
    }

aspx 文件如下:

 

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="DataList.aspx.cs" Inherits="test_DataList" %>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>021DataList</title>

</HEAD>
<body>
<script type="text/javascript">
function validate()
{
    
if (document.getElementById("txtPage").value=="")
        
{
            alert(
'不能爲空!');
            document.getElementById(
"txtPage").focus();
            
return false;
         }

    
         
        var digits
="0123456789";
        var temp;
        
for(i=0;document.getElementById("txtPage").value.length;i++)
            
{
                temp
=document.getElementById("txtPage").value.substring(i,i+1);
                
if (digits.indexOf(temp)==-1)
                    
{
                    alert(
"只能輸入數字!");
                    document.getElementById(
"txtPage").focus();
                    
return false;
            
                    }

                     
return true;
            }

           
      
    
return true;
}



   

</script>
<form id="Form1" method="post" runat="server">
<FONT face="宋體">
    
<asp:DataList ID="DataList1" runat="server">
        
<ItemTemplate>
            
<asp:Label ID="Label1" runat="server" Text='<%# Eval("printname") %>'></asp:Label>
        
</ItemTemplate>
    
</asp:DataList>
    
<asp:Label ID="Label2" runat="server" Text="Label"></asp:Label></FONT><asp:LinkButton ID="lbnPrevPage" runat="server" CommandName="Prev" OnCommand="Page_OnClick">上一頁</asp:LinkButton>
    
<asp:LinkButton ID="lbnNextPage" runat="server" CommandName="Next" OnCommand="Page_OnClick">下一頁</asp:LinkButton>
    
<asp:Label ID="lblRecordCount" runat="server" Text="Label"></asp:Label>
    
<asp:Label ID="lblPageCount" runat="server" Text="Label"></asp:Label>
    
<asp:LinkButton ID="lbnFirstPage" runat="server" CommandName="First" OnCommand="Page_OnClick">首頁</asp:LinkButton>
    
<asp:LinkButton ID="lbnLastPage" runat="server" CommandName="Last" OnCommand="Page_OnClick" Width="33px">末頁</asp:LinkButton>
    跳轉到
<asp:TextBox ID="txtPage" runat="server" Width="30px"></asp:TextBox>
    
<asp:Button ID="btnGo" runat="server" Text="Go" OnClick="btnGo_Click"   />
</form>
</body>
</HTML>

 

 

 

發佈了12 篇原創文章 · 獲贊 3 · 訪問量 4萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章