如何在 ASP.NET 中使用带有中继器控件的分页? [英] How to use paging with Repeater control in ASP.NET?

查看:14
本文介绍了如何在 ASP.NET 中使用带有中继器控件的分页?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 文章,你可以通过为页面创建另一个Repeater控件并使用来实现Repeater控件中的分页PagedDataSource 作为它的源.

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

<asp:Repeater ID="rptPaging" runat="server" OnItemCommand="rptPaging_ItemCommand"><项目模板><asp:LinkBut​​ton 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:LinkBut​​ton></ItemTemplate></asp:中继器>

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

//这个属性会包含当前页码公共整数页码{得到{if (ViewState["PageNumber"] != null){返回 Convert.ToInt32(ViewState["PageNumber"]);}别的{返回0;}}set { ViewState["PageNumber"] = value;}}

最后添加以下方法:

protected void Page_Load(object sender, EventArgs e){绑定中继器();}私有无效 BindRepeater(){//做你的数据库连接的东西并获取你的数据SqlConnection cn = new SqlConnection(yourConnectionString);SqlCommand cmd = 新的 SqlCommand();cmd.Connection = cn;SqlDataAdapter ad = new SqlDataAdapter(cmd);cmd.CommandText = "从你的表中选择 *";//将结果存入数据表数据表 dt = 新数据表();ad.SelectCommand = cmd;ad.Fill(dt);//创建将用于分页的PagedDataSourcePagedDataSource pgititems = new PagedDataSource();pgitems.DataSource = dt.DefaultView;pgititems.AllowPaging = true;//从这里控制页面大小pgititems.PageSize = 4;pgititems.CurrentPageIndex = 页码;如果(pgitems.PageCount > 1){rptPaging.Visible = true;ArrayList 页面 = new ArrayList();for (int i = 0; i <= pgititems.PageCount - 1; i++){pages.Add((i + 1).ToString());}rptPaging.DataSource = 页面;rptPaging.DataBind();}别的{rptPaging.Visible = false;}//最后,设置repeater的数据源RepCourse.DataSource = pgitems;RepCourse.DataBind();}//当点击页面没有来自寻呼机转发器的链接时将触发此方法protected void rptPaging_ItemCommand(对象源,System.Web.UI.WebControls.RepeaterCommandEventArgs e){PageNumber = Convert.ToInt32(e.CommandArgument) - 1;绑定中继器();}

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

替代解决方案

可以找到另一个出色的解决方案 此处,此解决方案包括页面导航按钮.您需要从该链接下载文件以查看功能分页,只需将 DataList 控件替换为您的 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>

In my project Repeater Control works fine. And now I need pagination for replacing those data. But I don't have any information about this. May be someone give me advice about this issue.

As shown below picture.

解决方案

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>

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.

Edit: Alternative Solution

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.

Hope this helps.

这篇关于如何在 ASP.NET 中使用带有中继器控件的分页?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆