DataList分页程序(转载)

转载他人的~~~也费了很大的劲~~~自己亲自编译通过
另外自己还加入自动编号的功能
效果图:

前台代码:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>

<!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>DataList分页程序</title>
    
<style type="text/css">
        .style1
        
{
            width: 784px;
        }

        .style2
        
{
            width: 126px;
        }

    
</style>
</head>
<body>
    
<form id="form1" runat="server">
    
<div align="center">
    
        
<asp:DataList ID="DataList1" runat="server" CellPadding="4" ForeColor="#333333" 
            Width
="784px" onitemcreated="DataList1_ItemCreated" 
            onitemdatabound
="DataList1_ItemDataBound">
            
<FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
            
<AlternatingItemStyle BackColor="White" />
            
<ItemStyle BackColor="#FFFBD6" ForeColor="#333333" />
            
<SeparatorStyle BorderStyle="Dashed" />
            
<SelectedItemStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="Navy" />
            
<HeaderTemplate>
                学生基本情况
<br />
                
<table class="style1" style="background-color: #808000">
                    
<tr class="style1">
                       
<td class="style2">
                            编号
</td>
                        
<td class="style2">
                            学号
</td>
                        
<td class="style2">
                            姓名
</td>
                        
<td class="style2">
                            性别
</td>
                        
<td class="style2">
                            年龄
</td>
                        
<td class="style2">
                            专业
</td>
                        
<td class="style2">
                            年级
</td>
                    
</tr>
                
</table>
            
</HeaderTemplate>
            
<HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
            
<ItemTemplate>
                
<table class="style1">
                    
<tr class="style1">
                          
<td class="style2">
                         
<%# (CurrentPage) * PageSize + Container.ItemIndex + 1%></td>
                        
<td class="style2"><%#DataBinder.Eval(Container.DataItem,"id"%></td>
                        
<td class="style2">
                           
<%#DataBinder.Eval(Container.DataItem,"name"%></td>
                        
<td class="style2">
                            
<%#DataBinder.Eval(Container.DataItem,"sex"%></td>
                        
<td class="style2">
                            
<%#DataBinder.Eval(Container.DataItem,"age"%></td>
                        
<td class="style2">
                            
<%#DataBinder.Eval(Container.DataItem,"department"%></td>
                        
<td class="style2">
                           
<%#DataBinder.Eval(Container.DataItem,"grade"%></td>
                    
</tr>
                
</table>
            
</ItemTemplate>
        
</asp:DataList>

            
<asp:linkbutton id="FirstLB" runat="server" OnCommand="LinkButton_Click" 
            CommandName
="first">第一页</asp:linkbutton>&nbsp;
            
<asp:linkbutton id="PreviousLB" runat="server" OnCommand="LinkButton_Click" CommandName="prev">上一页</asp:linkbutton>&nbsp;
            
<asp:linkbutton id="NextLB" runat="server" OnCommand="LinkButton_Click" CommandName="next">下一页</asp:linkbutton>&nbsp;
            
<asp:linkbutton id="EndLB" runat="server" OnCommand="LinkButton_Click" CommandName="end">最后一页</asp:linkbutton>&nbsp;&nbsp;
            总
<asp:label id="TotalLbl" runat="server"></asp:label>页 当前第<asp:label id="CurrentLbl" runat="server"></asp:label>
            
<asp:linkbutton id="JumpLB" runat="server" OnCommand="LinkButton_Click" CommandName="jump">跳到</asp:linkbutton>
            
<asp:textbox id="TextBox1" runat="server" Width="90px"></asp:textbox>
            页

    
</div>
    
</form>
</body>
</html>

后台代码:
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Data.SqlClient;

public partial class Default2 : System.Web.UI.Page
{
    
int CurrentPage;//当前页数
    int PageSize;   //每页条数
    int PageCount;  //总页数
    int RecordCount;//总条数
    private void Page_Load(object sender, System.EventArgs e)
    
{
        
// 在此处放置用户代码以初始化页面
        PageSize = 2;//每页2条记录
        if (!Page.IsPostBack)
        
{
            CurrentPage 
= 0;//当前页习惯设为0
            ViewState["PageIndex"= 0;//页索引也设为0
            
//计算总共有多少记录
            RecordCount = CalculateRecord();
            
//计算总共有多少页
            if (RecordCount % PageSize == 0)
            
{
                PageCount 
= RecordCount / PageSize;
            }

            
else
            
{
                PageCount 
= RecordCount / PageSize + 1;
            }

            
this.TotalLbl.Text = PageCount.ToString();//显示总页数
            ViewState["PageCount"= PageCount;//会话session 对整个 application 有效 ,而视图状态 viewstate相当于某个页面的 session
            this.DataListBind();//不可以放在初始化条件之前就绑定,那样的话,如果仅有一页的数据,“下一页”页仍然显示
        }

    }

    
//计算总共有多少条记录
    private int CalculateRecord()
    
{
        
try
        
{
            
int recordCount;
            SqlConnection con 
= new SqlConnection(ConfigurationManager.ConnectionStrings["Personal"].ConnectionString);//数据库使用Northwind;
            con.Open();
            
string sql = "select count(*) as count from information";
            SqlCommand cmd 
= new SqlCommand(sql, con);
            SqlDataReader sdr 
= cmd.ExecuteReader();
            
if (sdr.Read())
            
{
                recordCount 
= Int32.Parse(sdr["count"].ToString());
            }

            
else
            
{
                recordCount 
= 0;
            }

            sdr.Close();
            con.Close();
            
return recordCount;
        }

        
catch (Exception ex)
        
{
            
throw new Exception(ex.Message);
        }

    }

    
//将数据绑定到Datalist控件
    public void DataListBind()
    
{
        
try
        
{
            
int StartIndex = CurrentPage * PageSize;//设定导入的起终地址
            string sql = "select * from information";
            DataSet ds 
= new DataSet();
            SqlConnection con 
= new SqlConnection(ConfigurationManager.ConnectionStrings["Personal"].ConnectionString);
            con.Open();
            SqlDataAdapter sda 
= new SqlDataAdapter(sql, con);
            sda.Fill(ds, StartIndex, PageSize, 
"information");//这是sda.Fill方法的第一次重载,里面的变量分别是数据集DataSet ,开始记录数StartRecord,最大的记录数MaxRecord,数据表名TableName
            this.DataList1.DataSource = ds.Tables["information"].DefaultView;
            
this.DataList1.DataBind();
            
this.PreviousLB.Enabled = true;
            
this.NextLB.Enabled = true;
            
if (CurrentPage == (PageCount - 1))
            
{
                
this.NextLB.Enabled = false;//当为最后一页时,下一页链接按钮不可用
            }

            
if (CurrentPage == 0)
            
{
                
this.PreviousLB.Enabled = false;//当为第一页时,上一页按钮不可用
            }

            
this.CurrentLbl.Text = (CurrentPage + 1).ToString();//当前页数
        }

        
catch (Exception ex)
        
{
            
throw new Exception(ex.Message);
        }

    }

    
public void LinkButton_Click(Object sender, CommandEventArgs e)//自己编写的按钮点击事件
    {
        CurrentPage 
= (int)ViewState["PageIndex"];//获得当前页索引
        PageCount = (int)ViewState["PageCount"];//获得总页数
        string cmd = e.CommandName;
        
//判断cmd,以判定翻页方向
        switch (cmd)
        
{
            
case "prev"://上一页
                if (CurrentPage > 0)
                
{
                    CurrentPage
--;
                }

                
break;
            
case "next":
                
if (CurrentPage < (PageCount - 1))
                
{
                    CurrentPage
++;//下一页
                }

                
break;
            
case "first"://第一页
                CurrentPage = 0;
                
break;
            
case "end"://最后一页
                CurrentPage = PageCount - 1;
                
break;

            
case "jump"://跳转到第几页
                if (this.TextBox1.Text.Trim() == "" || Int32.Parse(this.TextBox1.Text.Trim()) > PageCount)//如果输入数字为空或超出范围则返回
                {
                    
return;
                }

                
else
                
{
                    CurrentPage 
= Int32.Parse(this.TextBox1.Text.ToString()) - 1;
                    
break;
                }

        }

        ViewState[
"PageIndex"= CurrentPage;//获得当前页
        this.DataListBind();//重新将DataList绑定到数据库
    }

}


posted @ 2008-03-25 14:42  MicroCoder  阅读(487)  评论(1编辑  收藏  举报