How to Learn Implement Pagination in Repeater Control in Asp.net

Description:-

In this article I am going to explain how to Learn Implement Pagination in Repeater Control in Asp.net

In this example I am using the Next, Previous, First and last button in pagination. I have created table Student and table have data.
As we know repeater control by default does not have pagination option. Hence if we use the repeater control and want pagination in it so we have to write the custom code for it.

Example of Implementation:
Add a web form to project. Drag and drop the Repeater control and 4 link button from toolbox to web form.

Default.aspx:-

<div>
  <table>
    <tr>
      <td>
        <asp:Repeater ID="rptsudentdetail" runat="server">
            <HeaderTemplate>
            <table>
              <thead>
                <th>Student Id</th>
                <th>Name</th>
                <th>DateofJoin</th>
              </thead>
            </HeaderTemplate>
            <ItemTemplate>
              <tr>
                <td><%#DataBinder.Eval(Container, "DataItem.stud_id")%></td>
                <td><%#DataBinder.Eval(Container, "DataItem.stud_name")%></td>
                <td><%#DataBinder.Eval(Container, "DataItem.dateofjoin")%></td>
              </tr>
            </ItemTemplate>
        </asp:Repeater>
      </td>
    </tr>
    <tr><td colspan="4"></td></tr>
    <tr><td></td></tr>
  </table>
  <table style="text-align: center;">
    <tr>
      <td><asp:LinkButton ID="lnkFirst" runat="server" CssClass="btncss" onclick="lnkFirst_Click"> << First </asp:LinkButton></td>
      <td><asp:LinkButton ID="lnkPrevious" runat="server" CssClass="btncss"                         onclick="lnkPrevious_Click">< Previous </asp:LinkButton></td>
      <td><asp:LinkButton ID="lnkNext" runat="server" CssClass="btncss" onclick="lnkNext_Click">Next > </asp:LinkButton></td>
      <td><asp:LinkButton ID="lnkLast" runat="server" CssClass="btncss"                        onclick="lnkLast_Click"> Last >></asp:LinkButton></td>
    </tr>
  </table>
</div>

Style:-

     <style>
        .btncss
        {
            padding: 8px;
            margin: 2px;
            background: #ffa100;
            border: solid1px#666;
            font: 8pttahoma;
            font-weight: bold;
            color: #000;
        }
        tabletrtd
        {
            text-align: center;
            margin: 5px;
        }
        tabletrth
        {
            margin: 5px;
            padding: 5px;
        }
    </style>

Default.aspx.cs:-

SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["DBCS"].ToString());
public static int totalPages = 0;
protected void Page_Load(object sender, EventArgs e)
{
  if (!IsPostBack)
  {
    BindRepeater();
  }
}
public int Pageno
{
  get
  {
    if (ViewState["pagenumber"] != null)
       return Convert.ToInt32(ViewState["pagenumber"]);
    else
       return 0;
  }
  set
  {
    ViewState["pagenumber"] = value;
  }
}

public void BindRepeater()
{
  try
  {
     SqlDataAdapter adp = new SqlDataAdapter("Select * from student", con);
     DataTable dt = new DataTable();
     adp.Fill(dt);
     PagedDataSource pagedatasource = new PagedDataSource();
     DataView dv = new DataView(dt);
     pagedatasource.DataSource = dv;
     pagedatasource.AllowPaging = true;
     pagedatasource.PageSize = 2;
     pagedatasource.CurrentPageIndex = Pageno;
     totalPages = pagedatasource.PageCount - 1;
     lnkPrevious.Enabled = !pagedatasource.IsFirstPage;
     lnkNext.Enabled = !pagedatasource.IsLastPage;
     lnkFirst.Enabled = !pagedatasource.IsFirstPage;
     lnkLast.Enabled = !pagedatasource.IsLastPage;
     ViewState["totpage"] = pagedatasource.PageCount;
     if (pagedatasource.PageCount > 1)
     {
       rptsudentdetail.DataSource = pagedatasource;
       rptsudentdetail.DataBind();
     }
  }
  catch (Exception ex)
  {
  }
}

protected void lnkFirst_Click(object sender, EventArgs e)
{
  lnkFirst.Attributes["style"] = "background-color:black;color:#fff";
  lnkPrevious.Attributes["style"] = "";
  lnkLast.Attributes["style"] = "";
  lnkNext.Attributes["style"] = "";
  Pageno = 0;
  BindRepeater();
}

protected void lnkLast_Click(object sender, EventArgs e)
{
  lnkLast.Attributes["style"] = "background-color:black;color:#fff";
  lnkFirst.Attributes["style"] = "";
  lnkPrevious.Attributes["style"] = "";
  lnkNext.Attributes["style"] = "";
  Pageno = (Convert.ToInt32(ViewState["totpage"]) - 1);
  BindRepeater();
}

protected void lnkNext_Click(object sender, EventArgs e)
{
  lnkNext.Attributes["style"] = "background-color:black;color:#fff";
  lnkFirst.Attributes["style"] = "";
  lnkPrevious.Attributes["style"] = "";
  lnkLast.Attributes["style"] = "";
  if (Pageno == totalPages)
  {
    Pageno = totalPages;
  }
  else
  {
    Pageno = Pageno + 1;
  }
  BindRepeater();
}

protected void lnkPrevious_Click(object sender, EventArgs e)
{
  lnkPrevious.Attributes["style"] = "background-color:black;color:#fff";
  lnkFirst.Attributes["style"] = "";
  lnkLast.Attributes["style"] = "";
  lnkNext.Attributes["style"] = "";
  if (Pageno == 0)
  {
    Pageno = 0;
  }
  else
  {
    Pageno = Pageno - 1;
  }
  BindRepeater();
}

Related Posts

Previous
Next Post »

Thanks for comments.....