c# - 如何在 ASP.NET 中使用 Repeater 控件进行分页?

标签 c# jquery asp.net pagination repeater

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

  <ItemTemplate>

    <div style="width:400px"></div>

    <div class="course" style="float: left; margin-left: 100px; margin-top: 100px">

      <div class="image">
        <asp:Image ID="imgteacher" runat="server" Height="150" Width="248" ImageUrl='<%# "ShowImage.ashx?id="+ DataBinder.Eval(Container.DataItem, "CourseID") %>'/>
      </div>

      <div style="margin-left: 3px; width: 250px">
        <div class="name">
          <a href="#"><asp:Label runat="server" ID="lblname" Text='<%#Eval("CourseName") %>'></asp:Label></a>
        </div>
        <div style="height: 13px"></div>
        <div id="teacher">
          <a href="#"><%#Eval("UserName") %> </a>
        </div>
      </div>

      <div style="height: 4px"></div>

      <div class="date">
        <div id="datebegin">
          <asp:Label ID="lbldatebegin" runat="server" Text='<%#Eval("BeginDate") %>'></asp:Label>
        </div>
        <div id="dateend">
          <asp:Label ID="lbldateend" runat="server" Text='<%#Eval("ClosingDate") %>'></asp:Label>
        </div>
      </div>

    </div>

  </ItemTemplate>

</asp:Repeater>

在我的项目中,Repeater Control 工作正常。现在我需要分页来替换这些数据。但我没有这方面的任何信息。可能有人给我关于这个问题的建议。

如下图所示。

enter image description here

最佳答案

Repeater 控件中没有内置分页,而是基于this。文章中,您可以通过为页面创建另一个 Repeater 控件并使用 PagedDataSource 作为它的源来在 Repeater 控件中实现分页。

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

<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>

接下来,在您的代码后面添加以下属性:

//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; }
}

最后添加如下方法:

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();
}

请试一试,如果您遇到任何问题,请告诉我。

编辑:替代解决方案

可以找到另一个优秀的解决方案Here ,此解决方案包括页面的导航按钮。您需要从该链接下载文件以查看功能分页,只需将 DataList 控件替换为您的 Repeater 控件即可。

希望这对您有所帮助。

关于c# - 如何在 ASP.NET 中使用 Repeater 控件进行分页?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22904666/

相关文章:

asp.net - IIS 8 发布 ASP.NET 核心应用程序 - 正在使用的文件

c# - CheckBoxList 项之间的线

c# - 使用 Windows 身份验证对单个操作而不是整个应用程序进行身份验证

c# - WCF REST 服务控制台主机

c# - 如何在 Excel 操作 Pane 中使用 WPF 控件?

c# - 在 AvalonEdit 中突出显示所有出现的选定单词

javascript - 使用 AJAX 为 AJAX 预加载图像

c# - 糊涂了,这是poco吗?

javascript - 使用 $.each() 进行自定义 JQuery 验证

javascript - jQuery全屏部分向左滑动,向右滑动