DataList翻頁

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="ucPicList.ascx.cs" Inherits="SMBB2C.Web.Products.UserControls.ucPicList" %>

<script type="text/javascript">
   function  ShowPic(obj)
   {
       var img=document.getElementById("<%=Image1.ClientID%>");
       img.src=obj.src;
   }
</script>
<table border="0" cellpadding="0" cellspacing="0">
    <tr>
    <td colspan="3"><asp:Image id="Image1" Width="220px" Height="133px" runat="server"></asp:Image></td>
    </tr>
    <tr>
        <td>
        <%--<asp:HyperLink ID="lnkPrev" runat="server" ToolTip="上一页"><<</asp:HyperLink>--%>
        </td>
        <td><asp:DataList ID="DataList1" runat="server" RepeatDirection="Horizontal">
    <ItemTemplate>
        <asp:Image ID="Image2" runat="server" Height="40px" ImageUrl='<%# Eval("Url") %>'
            onmousedown="ShowPic(this);" Width="60px" />
    </ItemTemplate>
</asp:DataList>
        </td>     
        <td>  <%--<asp:HyperLink ID="lnkNext" runat="server" ToolTip="下一页">>></asp:HyperLink>--%>
        </td>
    </tr>
</table>

方法1.

 public int RepeatColumns
        {
            get { return ViewState["ucPicList.RepeatColumns"] == null ? 3 : (int)ViewState["ucPicList.RepeatColumns"]; }
            set { ViewState["ucPicList.RepeatColumns"] = value; }
        }
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        public void View(string match)
        {
            BindDl(match);
        }
        protected void BindDl(string match)
        {
          
                string PhotoPath = getPath();              
                System.IO.DirectoryInfo d = new System.IO.DirectoryInfo(PhotoPath);
                FileInfo[] fs = d.GetFiles(match + "-" + "*");
                //int i = 0;
                DataTable dtPhoto = new DataTable();
                dtPhoto.Columns.Add("Url", typeof(System.String));
                // dtPhoto.Columns.Add("Name", typeof(System.String));
                //  dtPhoto.Columns.Add("Key", typeof(System.String));
                foreach (FileInfo f in fs)
                {
                    //  i++;
                    DataRow r = dtPhoto.NewRow();
                    // r["Key"] = f.Name;
                    r["Url"] = "http://www.cnblogs.com/Upload/ProductPic/" + Server.UrlEncode(f.Name);
                    // r["Name"] = "图" + i.ToString();
                    dtPhoto.Rows.Add(r);
                }

                PagedDataSource pds = new PagedDataSource();
                pds.DataSource = dtPhoto.DefaultView;

                pds.AllowPaging = true;
                pds.PageSize = RepeatColumns;
                int curPage;
                if (Request.QueryString["Page"] != null)
                {
                    curPage = Int32.Parse(Request.QueryString["Page"].ToString());
                }
                else
                {
                    curPage = 1;
                }
                pds.CurrentPageIndex = curPage - 1;

                        if (!pds.IsFirstPage)
                {
                 this.lnkPrev.NavigateUrl = Request.CurrentExecutionFilePath + "?Page=" + Convert.ToString(curPage - 1);
                }
                if (!pds.IsLastPage)
               {
                  this.lnkNext.NavigateUrl = Request.CurrentExecutionFilePath + "?Page=" + Convert.ToString(curPage + 1);                    
               }

                Image1.ImageUrl = "http://www.cnblogs.com/Upload/ProductPic/" + fs[0].Name;
                this.DataList1.DataSource = pds;
                this.DataList1.DataBind();            
               
        }


        private string getPath()
        {
            return System.Web.HttpContext.Current.Server.MapPath("~/Upload/ProductPic/");
        }

方法2.

 public int RepeatColumns
        {
            get { return ViewState["ucPicList.RepeatColumns"] == null ? 3 : (int)ViewState["ucPicList.RepeatColumns"]; }
            set { ViewState["ucPicList.RepeatColumns"] = value; }
        }
        int CurrentPageIndex
        {
            get { return ViewState["ucPicList.CurrentPageIndex"] == null ? 0 : (int)ViewState["ucPicList.CurrentPageIndex"]; }
            set { ViewState["ucPicList.CurrentPageIndex"] = value; }
        }
        string CurrentMatch
        {
            get { return ViewState["ucPicList.CurrentMatch"] == null ? "" : ViewState["ucPicList.CurrentMatch"].ToString(); }
            set { ViewState["ucPicList.CurrentMatch"] = value; }
        }
        protected void Page_Load(object sender, EventArgs e)
        {

        }      
      
        public void ViewByMatch(string match)
        {
            CurrentMatch = match;
            DataTable dt = getPics(match);
            View(dt);
        }
        protected void View(DataTable dt)
        {
            CurrentPageIndex = 0;          
            BindDlPics(dt);
            int PageCount = dt.Rows.Count / PageSize + 1;
            setPager(CurrentPageIndex, PageCount);

        }
      
        protected void BindDlPics(DataTable dt)   
        {           
            int StartIndex=CurrentPageIndex*RepeatColumns;
            DataTable SubDt = getSubDt(dt, StartIndex, RepeatColumns);

            this.DataList1.DataSource = SubDt;
            this.DataList1.DataBind();

        }
        protected DataTable getSubDt(DataTable dt, int StartIndex, int RecordCounts)
        {
            if (dt.Rows.Count != 0 && StartIndex < dt.Rows.Count)
            {
                DataTable SubDt = dt.Clone();

                for (int i = StartIndex; i < StartIndex + RecordCounts; i++)
                {
                    SubDt.ImportRow(dt.Rows[i]);
                    if (i == dt.Rows.Count - 1) break;
                }
                return SubDt;
            }
            else
            {
                return null;
            }
        }
        private string getPath()
        {
            return System.Web.HttpContext.Current.Server.MapPath("~/Upload/ProductPic/");
        }
        protected DataTable getPics(string match)
        {
            string PhotoPath = getPath();
            System.IO.DirectoryInfo d = new System.IO.DirectoryInfo(PhotoPath);
            FileInfo[] fs = d.GetFiles(match + "-" + "*");
            //int i = 0;
            DataTable dtPhoto = new DataTable();
            dtPhoto.Columns.Add("Url", typeof(System.String));
            // dtPhoto.Columns.Add("Name", typeof(System.String));
            //  dtPhoto.Columns.Add("Key", typeof(System.String));
            foreach (FileInfo f in fs)
            {
                //  i++;
                DataRow r = dtPhoto.NewRow();
                // r["Key"] = f.Name;
                r["Url"] = "http://www.cnblogs.com/Upload/ProductPic/" + Server.UrlEncode(f.Name);
                // r["Name"] = "图" + i.ToString();
                dtPhoto.Rows.Add(r);
            }
          if(fs.Length!=0)
            Image1.ImageUrl = "http://www.cnblogs.com/Upload/ProductPic/" + fs[0].Name;
            return dtPhoto;
        }

        protected void setPager(int CurrentPageIndex,int PageCount)
        {
            lnkbtnNext.Visible = true;
            lnkbtnPre.Visible = true;
            if (PageCount == 0 || PageCount == 1)
            {
                lnkbtnNext.Visible = false;
                lnkbtnPre.Visible = false;
            }
            else if (CurrentPageIndex == 0)
            {
                lnkbtnPre.Visible = false;
            }
            else if (CurrentPageIndex == PageCount - 1)
            {
                lnkbtnNext.Visible = false;
            }
        }

        protected void lnkbtnNext_Click(object sender, EventArgs e)
        {
            CurrentPageIndex += 1;
            DataTable dt = getPics(CurrentMatch);
            BindDlPics(dt);
             int PageCount = dt.Rows.Count / PageSize + 1;
            setPager(CurrentPageIndex, PageCount);
        }

        protected void lnkbtnPre_Click(object sender, EventArgs e)
        {
            CurrentPageIndex -= 1;
            DataTable dt = getPics(CurrentMatch);
            BindDlPics(dt);
             int PageCount = dt.Rows.Count / PageSize + 1;
            setPager(CurrentPageIndex, PageCount);
        }


原文地址:https://www.cnblogs.com/yangfan/p/1206332.html