且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用分页与ASP.NET Web窗体Repeater控件

更新时间:2023-12-06 10:19:22

有没有内置在Repeater控件分页,但基于this文章中,你可以通过创建页面另一Repeater控件实现Repeater控件中的分页和使用PagedDataSource,因为它的来源。

There's no built-in pagination in the Repeater control, but based on this article, you can achieve pagination in the Repeater control by creating another Repeater control for pages and use PagedDataSource as it's source.

首先,将它添加到您的标记:

First, add this to your markup:

   <div style="overflow: hidden;">
        <asp:Repeater ID="rptPaging" runat="server" OnItemCommand="rptPaging_ItemCommand">
            <ItemTemplate>
                    <asp:LinkButton ID="btnPage"
        style="padding:8px; margin:2px; background:#ffa100; border:solid 1px #666; font:8pt tahoma;"
    CommandName="Page" CommandArgument="<%# Container.DataItem %>"
        runat="server" ForeColor="White" Font-Bold="True"><%# Container.DataItem %>
                    </asp:LinkButton>
           </ItemTemplate>
        </asp:Repeater>
   </div>

接下来,在你的code后面添加以下属性:

Next, add the following property in your code behind:

   //This property will contain the current page number 
    public int PageNumber
    {
        get
        {
            if (ViewState["PageNumber"] != null)
            {
                return Convert.ToInt32(ViewState["PageNumber"]);
            }
            else
            {
                return 0;
            }
        }
        set { ViewState["PageNumber"] = value; }
    }

最后添加以下方法:

Finally add the following methods:

protected void Page_Load(object sender, EventArgs e)
{
    BindRepeater();
}

private void BindRepeater()
{
    //Do your database connection stuff and get your data
    SqlConnection cn = new SqlConnection(yourConnectionString);
    SqlCommand cmd = new SqlCommand();
    cmd.Connection = cn;
    SqlDataAdapter ad = new SqlDataAdapter(cmd);
    cmd.CommandText = "Select * from YourTable";

    //save the result in data table
    DataTable dt = new DataTable();
    ad.SelectCommand = cmd;
    ad.Fill(dt);

    //Create the PagedDataSource that will be used in paging
    PagedDataSource pgitems = new PagedDataSource();
    pgitems.DataSource = dt.DefaultView;
    pgitems.AllowPaging = true;

    //Control page size from here 
    pgitems.PageSize = 4;
    pgitems.CurrentPageIndex = PageNumber;
    if (pgitems.PageCount > 1)
    {
        rptPaging.Visible = true;
        ArrayList pages = new ArrayList();
        for (int i = 0; i <= pgitems.PageCount - 1; i++)
        {
            pages.Add((i + 1).ToString());
        }
        rptPaging.DataSource = pages;
        rptPaging.DataBind();
    }
    else
    {
        rptPaging.Visible = false;
    }

    //Finally, set the datasource of the repeater
    RepCourse.DataSource = pgitems;
    RepCourse.DataBind();
}

//This method will fire when clicking on the page no link from the pager repeater
protected void rptPaging_ItemCommand(object source, System.Web.UI.WebControls.RepeaterCommandEventArgs e)
{
    PageNumber = Convert.ToInt32(e.CommandArgument) - 1;
    BindRepeater();
}

请给它一个尝试,如果你遇到任何问题,只是通知我。

Please give it a try and if you faced any issue just inform me.

编辑:另一种解决方案

另一个出色的解决方案,可以发现Here,该解决方案包括的网页导航按钮。你需要从该链接下载文件,看的功能和分页只需更换您的Repeater控件DataList控件。

Another excellent solution can be found Here, this solution includes the Navigation buttons of pages. You'll need to download files from that link to see a functional pagination and just replace the datalist control with your Repeater control.

希望这有助于。