DataList控件分页

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.OleDb;
using System.Configuration;
using System.Data;


public partial class datalist : System.Web.UI.Page
{
    static PagedDataSource pds = new PagedDataSource();//创建一个分页数据源对象,声明为静态
    static string str1 = ConfigurationManager.ConnectionStrings["accessconn"].ConnectionString.ToString();  //读取节点配置的字符
    OleDbConnection connstr1 = new OleDbConnection(str1);  //建立数据库连接对象
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack) 
        {
            BindData(0);
        }
    }
    protected void BindData(int currentpage)//自定义函数绑定数据到控件
    {
        pds.AllowPaging = true;//起用分页
        pds.PageSize = 3;//单页上显示的项数
        pds.CurrentPageIndex = currentpage;
        if (connstr1.State == System.Data.ConnectionState.Closed)  //打开数据库
        {
            connstr1.Open();
        }
        //string parameter = Request.QueryString["type"].ToString();
        //string content = "select * from flim_info where ftype='" + parameter + "'";  //编写SQL语句
        string content = "select * from flim_info";
        OleDbDataAdapter oledba = new OleDbDataAdapter(content, connstr1);//创建数据适配器
        DataSet ds = new DataSet(); //创建数据集
        oledba.Fill(ds);  //填充数据集
        pds.DataSource = ds.Tables[0].DefaultView;//把数据集中的数据放到分页数据源中
        dl1.DataSource = pds; //设置datalist的数据源为创建的数据集pds
        dl1.DataBind();  //数据绑定


        if (connstr1.State == System.Data.ConnectionState.Open)  //关闭数据库
        {
            connstr1.Close();
        }


    }
    protected void dl1_ItemDataBound(object sender, DataListItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Footer)
        {
            //为得到脚模板中的控件,并创建变量
            Label pagecount = e.Item.FindControl("totalpages") as Label;
            Label currentpage = e.Item.FindControl("presentpage") as Label;           
            Button firstpage = e.Item.FindControl("bthome") as Button;
            Button prepage = e.Item.FindControl("btpre") as Button;
            Button nextpage = e.Item.FindControl("btnext") as Button;
            Button lastpage = e.Item.FindControl("btlast") as Button;
            currentpage.Text = (pds.CurrentPageIndex +1).ToString();//绑定显示当前页数
            pagecount.Text = pds.PageCount.ToString();//绑定显示总页数
            if (pds.IsFirstPage) //假如当前页是第一页,则首页和上一页不能用
            {
                firstpage.Enabled = false;
                prepage.Enabled = false;
            }
            if (pds.IsLastPage) //假如当前页是最后一页,则尾页和下一页不能用
            {
                lastpage.Enabled = false;
                nextpage.Enabled = false;
            }
        }
    }
    protected void dl1_ItemCommand(object source, DataListCommandEventArgs e)
    {
        switch(e.CommandName)//判断与此控件关联的命令
        {
            case "home":                                 //首页
                pds.CurrentPageIndex = 0;
                BindData(pds.CurrentPageIndex);
                break;
            case "last":                                 //尾页
                pds.CurrentPageIndex = pds.PageCount- 1;
                BindData(pds.CurrentPageIndex);
                break;
            case "back":                                    //上一页
                pds.CurrentPageIndex = pds.CurrentPageIndex - 1;
                BindData(pds.CurrentPageIndex);
                break;
            case "next":                                    //下一页
                pds.CurrentPageIndex = pds.CurrentPageIndex + 1;
                BindData(pds.CurrentPageIndex);
                break;
            case "search":                                 //跳转
                if (e.Item.ItemType == ListItemType.Footer)
                {
                    int pagecount = int.Parse(pds.PageCount.ToString());///取得总页数
                    TextBox txtpage = e.Item.FindControl("tbpage") as TextBox;//取得文本框对象
                    int mypagenum = 0;                          //设置一个中间变量,接收要跳转的页数
                    if (!txtpage.Text.Equals("")) 
                    {
                        mypagenum =Convert.ToInt32( txtpage.Text.ToString());
                    }
                    if (mypagenum <= 0 || mypagenum > pagecount) 
                    {
                        Response.Write("<script>alert('请输入页数,且不得超出总页数!')</script>");
                    }
                    else 
                    {
                        BindData(mypagenum-1);//注意:物理、逻辑顺序不同
                    }  
                }
                break;


        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值