PageDataSource 分页

using System;
using System.Data;
using System.Configuration;
using System.Collections;
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.SqlClient;
using System.Collections.Generic;

public partial class DataLlist : System.Web.UI.Page
{
     //static int pagen=0;//当前第 pagen+1 页

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            MyDs();
            ViewState["pagen"] = 0;//当前第 pagen+1 页
        }
    }

    private void MyDs()
    {
      
        try
        {
           // int s = pagen * 10;
            SqlConnection con = DBGet.Getcon();
           
            //子查询分页 (一)

            //string sql = "select top 5 * from books  where id not in( select top " + s + " id from books order by id  )";
            //SqlDataAdapter adapter = new SqlDataAdapter(sql, con);
            //DataSet ds = new DataSet();
            //adapter.Fill(ds, "books");

            //this.dataList.DataSource = ds.Tables[0].DefaultView;
            //this.dataList.DataBind();


            //pagedataSource 分页
            string sql = "select  * from books";//所有数据
            SqlDataAdapter adapter = new SqlDataAdapter(sql, con);
            DataSet ds = new DataSet();
            adapter.Fill(ds, "books");

            PagedDataSource pgds = new PagedDataSource();//
            pgds.DataSource = ds.Tables[0].DefaultView;//数据绑定
            pgds.AllowPaging = true;//允许分页
            pgds.PageSize = 6;//每页数据量
            pgds.CurrentPageIndex = (Convert.ToInt32(ViewState["pagen"]));//设置显示当前第几页
          
            SetEnable(pgds);//是否可点??
           
            this.dataList.DataSource =pgds ;
            this.dataList.DataBind();
        }
        catch (SqlException ex)
        {
            throw ex;
        }
    }
    private void SetEnable(PagedDataSource pgds)//设置是否可点
    {
        this.lbtnpre.Enabled = true;
        this.lbtnNext.Enabled = true;
     if(pgds.IsFirstPage)
     {
         this.lbtnpre.Enabled = false;

     }
        if(pgds.IsLastPage)
        {
            this.lbtnNext.Enabled = false;
        }
    }
    public string  GetUrl(Object temp)//图片路径
    {
     
        string path = "Images/9787900107954.jpg";
        if (temp != null)
        {
            string imgpath = "Images/" + temp.ToString() + ".jpg";
            path = imgpath;
        }
        return path;
    }
    public string MySub(object str,int n) //截取字符串
    {
        string s = null;
     if(str!=null )
     {
         if (str.ToString().Length > n)//检查是否大于指定长度
         {
              s = str.ToString().Substring(0, n) + "...";
         }
         else
         {
             s = str.ToString();
         }
     }
     return s;
    }
    protected void LinkButton1_Click(object sender, EventArgs e)
    {
        //上一页
        //pagen--;

       ViewState["pagen"]= ((int)ViewState["pagen"])-1;//改变页码
        MyDs();
    }
    protected void LinkButton2_Click(object sender, EventArgs e)
    {
        //下一页
        ViewState["pagen"] = ((int)(ViewState["pagen"]))+1; 
        MyDs();
    }
    protected void dataList_SelectedIndexChanged(object sender, EventArgs e)
    {

    }
}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

前台///

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

<!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>无标题页</title>
    <style type="text/css">
     *{
     font-size:12px ;
     }
     .myDiv{
         background-color:yellow ;
     }
    </style>
</head>

<body style="text-align: center">
    <form id="form1" runat="server">
    <div style="text-align: center"  class="myDiv">
        <asp:DataList ID="dataList" runat="server" RepeatColumns="2" RepeatDirection="Horizontal" OnSelectedIndexChanged="dataList_SelectedIndexChanged">
            <ItemTemplate>
                <table border="0" cellpadding="0" cellspacing="0" width="500">
                    <tr>
                        <td colspan="2">
                            书的列表</td>
                    </tr>
                    <tr>
                        <td align="center" style="width: 200px;" rowspan="4">
                            <img src='<%# GetUrl(Eval("ISBN"))  %>' width="140" alt='<%#Eval("ISBN") %>' /></td>
                        <td align="left" style="width: 400px; height: 19px">
                            书名:<%#Eval("Title") %></td>
                    </tr>
                    <tr>
                        <td align="left" style="width:400px; height: 19px">
                        </td>
                    </tr>
                    <tr>
                       
                        <td align="left" style="width: 400px; height: 19px">
                           <a href="DataLlist2.aspx?ISBN=<%#Eval("ISBN") %>"> 简介:<%# MySub(Eval("ContentDescription"),200)%></a></td>
                    </tr>
                    <tr>
                        <td align="left" style="width: 400px; height: 19px; text-align: right">
                            价格:<%# Eval("UnitPrice")%></td>
                    </tr>
                </table>
                &nbsp;&nbsp;
            </ItemTemplate>
        </asp:DataList></div>
        <asp:LinkButton ID="lbtnpre" runat="server" OnClick="LinkButton1_Click">上一页</asp:LinkButton><asp:LinkButton
            ID="lbtnNext" runat="server" OnClick="LinkButton2_Click">下一页</asp:LinkButton>
    </form>
</body>
</html>

package com.model; import javax.servlet.http.HttpServletRequest; import java.util.*; /** * 说明:参数封装Map * @version */ public class PageData extends HashMap implements Map{ private static final long serialVersionUID = 1L; Map map = null; HttpServletRequest request; public PageData(HttpServletRequest request){ this.request = request; Map properties = request.getParameterMap(); Map returnMap = new HashMap(); Iterator entries = properties.entrySet().iterator(); Entry entry; String name = ""; String value = ""; while (entries.hasNext()) { entry = (Entry) entries.next(); name = (String) entry.getKey(); Object valueObj = entry.getValue(); if(null == valueObj){ value = ""; }else if(valueObj instanceof String[]){ String[] values = (String[])valueObj; for(int i=0;i<values.length;i++){ value = values[i] + ","; } value = value.substring(0, value.length()-1); }else{ value = valueObj.toString(); } returnMap.put(name, value); } map = returnMap; } public PageData() { map = new HashMap(); } @Override public Object get(Object key) { Object obj = null; if(map.get(key) instanceof Object[]) { Object[] arr = (Object[])map.get(key); obj = request == null ? arr:(request.getParameter((String)key) == null ? arr:arr[0]); } else { obj = map.get(key); } return obj; } public static boolean hasVal(PageData pd,String key){ return pd!=null&&pd;.containsKey(key)&&!pd.getString(key).isEmpty(); } public String getString(Object key) { return (String)get(key); } public Integer getInt(Object key) { return Integer.parseInt(getString(key)); } @SuppressWarnings("unchecked") @Override public Object put(Object key, Object value) { return map.put(key, value); } @Override public Object remove(Object key) { return map.remove(key); } public void clear() { map.clear(); } public boolean containsKey(Object key) { // TODO Auto-generated method stub
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值