datalist实现分页

分页效果如下:分页

 

后台代码:code  C#

一 、申明全局变量:

 public static int startIndex =0;      //起始页
    public static  int pagesize =8;      //每页记录数
    public static int pagecount =1;       //总记录
    public static int countpage = 0;           //总页数
    public static int nowpage = 1;            //当前页
    public static   DataSet DS = new DataSet();       //接收数据
    public static string   connstring="Data Source=.//SQLEXPRESS;AttachDbFilename=|DataDirectory|//payforboos.mdf;Integrated Security=True;User Instance=True";   //连接字符串
    static SqlConnection nwindConn = new SqlConnection(connstring);      
    public static   SqlDataAdapter adapter = new SqlDataAdapter("", nwindConn);
    static SqlCommand selCmd = adapter.SelectCommand;    //为查询适配器创建查询语句

 

 protected void Page_Load(object sender, EventArgs e)
    {
     
        if (!IsPostBack)
        {

           selCmd.CommandText = SqlDataSource1.SelectCommand.ToString();
            getdatatable();
            DataList1.DataSource = DS.Tables["allbooks"].DefaultView;
            DataList1.DataBind();
                DataTable dt = new DataTable();
            adapter.Fill(dt);
            pagecount = dt.Rows.Count;
            ViewState["pagecount"] = pagecount;
            countpage = (int)Math.Ceiling((double) pagecount / pagesize);   //计算页数    向上取整
        }

}

static public void  getdatatable()             //自定义函数
    {

       nwindConn.Close();

       nwindConn.Open();
        DS.Clear();      //将dataset中的数据清空   否则会形成累计
        selCmd.Parameters.Clear();
        adapter.Fill(DS, startIndex, pagesize, "allbooks");          //从第startindex条数据开始 读取pagesize条数据     添加到DS中

      nwindConn.Close();
    }

 

protected void Button3_Click(object sender, EventArgs e)        //下一页
    {
        if (nowpage <countpage  )
        {
            startIndex = startIndex + pagesize;
            nowpage++;
        }
        
          selCmd.CommandText = SqlDataSource1.SelectCommand.ToString();
           getdatatable();
           DataList1.DataSource = DS.Tables["allbooks"];
          DataList1.DataBind();
  }

protected void Button2_Click(object sender, EventArgs e)    //上一页
    {
        if (nowpage  > 1 )
        {
            startIndex -= pagesize;
            nowpage--;
        }  
        selCmd.CommandText = SqlDataSource1.SelectCommand.ToString();
        getdatatable();
         DataList1.DataSource = DS.Tables["allbooks"];
        DataList1.DataBind();

    }

 protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e)           //添加页码
    {
          foreach (Control c in e.Item.Controls)            //查询在footerTemplate                                 
        {
            DropDownList dplist = c as DropDownList;
            if (dplist != null)
            {
               
                for (int i = 1; i <=countpage ; i++)
                {
                    dplist.Items.Add(i.ToString());
                    
                 }
              
             }
        }

 

   protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)          //查找第I 页
    {
        DropDownList dplist = (DropDownList)sender;                   //获取sender对象中的dropdownlist的值
        int selectpage = int.Parse(dplist.SelectedValue.ToString());
        nowpage = selectpage;
        startIndex = (nowpage - 1) * pagesize;
        getdatatable();
        DataList1.DataSource = DS.Tables["allbooks"];
        DataList1.DataBind();
    }

 

 

前台代码:

  <FooterTemplate>
                    <table class="footstyle">
                        <tr>
                            <td>
                                总共<b> &nbsp;<%=countpage %> &nbsp;</b>页/当前第<b> &nbsp;<%=nowpage  %> &nbsp;</b>页</td>
                            <td>
                                总计<b> &nbsp;<%=pagecount  %> &nbsp;</b>条记录</td>
                            <td align="right">
                                <asp:Button ID="Button2" runat="server" οnclick="Button2_Click" Text="上一页" />
                                <asp:DropDownList ID="DropDownList1" runat="server" Height="19px" Width="58px"
                                    AutoPostBack="True"
                                    onselectedindexchanged="DropDownList1_SelectedIndexChanged"  >
                                 
                                </asp:DropDownList>
                                <asp:Button ID="Button3" runat="server" οnclick="Button3_Click" Text="下一页" />
                            </td>
                        </tr>
                    </table>
                </FooterTemplate>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值