Repeater无法从数据库访问

时间:2016-04-26 17:40:12

标签: asp.net repeater

Repeater无法从数据库访问,如何设置当前显示图像,例如,如果我有20个图像,对于第一页我只想显示10个图像。现在我将从文件夹中获取整个图像。

    <asp:Repeater ID="Repeater1" runat="server" >

        <ItemTemplate>

       <asp:HyperLink ID="HyperLink1"  ImageUrl='<%#"Images/" + Eval("Name") %>' CssClass="img img-responsive thumbnail center-block " runat="server" width="234px" height="250px"  ToolTip="check to buy" />

        </ItemTemplate>

    </asp:Repeater>
  </div>

上一页 下一页    

 private void ListImages(int SelectedPage)
    {
        DirectoryInfo dir = new DirectoryInfo(Server.MapPath("~/Images"));
        FileInfo[] file = dir.GetFiles();
        ArrayList list = new ArrayList();
        foreach (FileInfo info in file)
        {
            if (info.Extension == ".jpg" || info.Extension == ".jpeg" || info.Extension == ".gif" || info.Extension == ".png")
            {
                list.Add(info);

            }
        }


        Repeater1.DataSource = list;
        Repeater1.DataBind();

           // Step 2: Create PagedDataSource instance and set its properties
           PagedDataSource pds = new PagedDataSource();
           pds.DataSource = list;
           pds.AllowPaging = true;
           pds.PageSize = 2;
           if (SelectedPage > (pds.PageCount - 1))
           {
             SelectedPage = pds.PageCount - 1;
           }
            if (SelectedPage < 0)
             SelectedPage = 0;
           pds.CurrentPageIndex = SelectedPage;

           // Step 3: Bind PagedDataSource to Repeater and set LinkButtons' behavior
           Repeater1.DataSource = pds;
           if (pds.IsLastPage)
               lbNext.Enabled = false;
           else
               lbNext.Enabled = true;

           if (pds.IsFirstPage)
               lbPrevious.Enabled = false;
           else
               lbPrevious.Enabled = true;

    }

1 个答案:

答案 0 :(得分:0)

您需要将PageSize设置为10并将DataBind()设置为转发器:

// Step 2: Create PagedDataSource instance and set its properties
            PagedDataSource pds = new PagedDataSource();
            pds.DataSource = list;
            pds.AllowPaging = true;
            pds.PageSize = 10; //Display 10 images in each page
            if (SelectedPage > (pds.PageCount - 1))
            {
                SelectedPage = pds.PageCount - 1;
            }
            if (SelectedPage < 0)
                SelectedPage = 0;
            pds.CurrentPageIndex = SelectedPage;

            // Step 3: Bind PagedDataSource to Repeater and set LinkButtons' behavior
            Repeater1.DataSource = pds;
            Repeater1.DataBind(); //Like this