Share Blog

Saturday, February 28, 2015

How To Use Pagging In Asp.net Repeater Example

-------------------------Create Table For  New Headlines----------------





----------Pagging.aspx Code---------
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Pagging.aspx.cs" Inherits="Pagging" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
     <fieldset style="width:310px">
    <legend>Custom paging in Repeater</legend>
    <asp:Repeater ID="rptBooks" runat="server">
     <HeaderTemplate>
           <table style=" border:1px solid #20B2AA; width:300px" cellpadding="0">
<tr style="background-color:#20B2AA; color:White">
<td colspan="2">
<b><center>NEWS Information</center></b>
</td>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr style="background-color:#336699">
<td>
<table style="background-color:#f7f7f7;border-top:1px dotted #c1650f; width:300px" >
<tr>
<td>
<b>Name:</b>
<asp:Label ID="lblBookName" runat="server" Text='<%#Eval("Username") %>'/>
</td>
</tr>
<tr>
<td>
<b>Heading:</b>
<asp:Label ID="lblAuthor" runat="server" Text='<%#Eval("Heading") %>'/>
</td>
</tr>
<tr>
<td>
<b>Discription:</b>
<asp:Label ID="lblPublisher" runat="server" Text='<%#Eval("Discription") %>'/>
</td>
</tr>
<tr>
<td>
<b>Date:</b>
<asp:Label ID="lblPrice" runat="server" Text='<%#Eval("Posteddate") %>'/>
</td>
</tr>
</table>
</td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
    </asp: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:#20B2AA; 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> 
    </fieldset></div>
    </form>
</body>

</html>

------Pagging.aspx.cs Code--------


using System;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;
using System.Collections;
public partial class Pagging : System.Web.UI.Page
{

    SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["connn"].ConnectionString);
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindRepeater();
        }

    }
    protected void BindRepeater()
    {
        //SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["conStr"].ConnectionString.ToString());
        SqlCommand cmd = new SqlCommand("Select * from news_headlines", con);
        if (con.State == ConnectionState.Closed)
        {
            con.Open();
        }
        DataTable dt = new DataTable();
        SqlDataAdapter adp = new SqlDataAdapter(cmd);
        adp.Fill(dt);
        PagedDataSource pgitems = new PagedDataSource();
        DataView dv = new DataView(dt);
        pgitems.DataSource = dv;
        pgitems.AllowPaging = true;
        pgitems.PageSize = 2;
        pgitems.CurrentPageIndex = PageNumber;
        if (pgitems.PageCount > 1)
        {
            rptPaging.Visible = true;
            ArrayList pages = new ArrayList();
            for (int i = 0; i < pgitems.PageCount; i++)
                pages.Add((i + 1).ToString());
            rptPaging.DataSource = pages;
            rptPaging.DataBind();
        }
        else
        {
            rptPaging.Visible = false;
        }
        rptBooks.DataSource = pgitems;
        rptBooks.DataBind();
    }
    public int PageNumber
    {
        get
        {
            if (ViewState["PageNumber"] != null)
                return Convert.ToInt32(ViewState["PageNumber"]);
            else
                return 0;
        }
        set
        {
            ViewState["PageNumber"] = value;
        }
    }

   
    protected void rptPaging_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        PageNumber = Convert.ToInt32(e.CommandArgument) - 1;
        BindRepeater();
    }
}

RESULT