Friday, 9 August 2013

How to Create Paging or Custom Paging in Repeater Control in Asp.Net




Description:-

Earlier this section we already Explain How to Create Paging in Gridview. Now I recently had to use the Repeater control with large Number of data, and the only way to do this properly is to page through the data. Repeater Control does not provide Paging Facility Directally same like gridview. So to overcome this problem we use  a scenario where you can use the PagedDataSource class. This class encapsulates the paging related properties of a data-bound control. 

This is very Useful program like when you have large amount of Data you have Display in Repeater Control at that time You can put the thousand of Record in repeater control using Paging Facility in Repeater.

Paging means bunch of Record that we will display in one Pageview with Given Next and Previous Link.

There is a code that Demostrate How to Create Custom Paging in Repeater Control in Asp.Net

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;
        }
            RepDetails.DataSource = pgitems;
            RepDetails.DataBind();  

The PagedDataSource is a class that  you can easily use for create custom paging. And using this with LINQ’s Take and Skip methods is a great way to create paging that is highly efficient.

You Have to First Create a Table in database with name is comment.


to Show Example of For Insert,Update,Delete in Reapeter so please Click Here Insert update delete operation in Reapeter in Asp.Net

To Show Example of Create Paging in Gridview Click Here Paging in Gridview



repeter.aspx:-



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

<!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 id="Head1" runat="server">
<title>Repeator Control Example</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<table>
<tr>
<td>Enter Name: </td>
<td><asp:TextBox ID="txtName" runat="server"/></td>
</tr>

<tr>
<td valign="top">Enter Comments:</td>
<td><asp:TextBox ID="txtComment" runat="server" Rows="5" Columns="20" TextMode="MultiLine"/></td>
</tr>
<tr>
<td></td>
<td><asp:Button ID="btnSubmit" runat="server" Text="Submit" onclick="btnSubmit_Click" /></td>
</tr>
</table>
</div>
<div>
<asp:Repeater ID="RepDetails" runat="server">
<HeaderTemplate>
<table style=" border:1px solid #df5015; width:500px" cellpadding="0">
<tr style="background-color:#df5015; color:White">
<td colspan="2">
<b>Comments</b>
</td>
</tr>
</HeaderTemplate>
<ItemTemplate>

<tr>
<td>
<asp:Label ID="lblComment" runat="server" Text='<%#Eval("comment") %>'/>
   
</td>
</tr>
<tr>
<td>
<table style="background-color:#EBEFF0;border-top:1px dotted #df5015;border-bottom:1px solid #df5015; width:500px" >
<tr>
<td>Post By: <asp:Label ID="lblUser" runat="server" Font-Bold="true" Text='<%#Eval("unm") %>'/>
 
</td>
<td>Created Date:<asp:Label ID="lblDate" runat="server" Font-Bold="true" Text='<%#Eval("postdate") %>'/></td>
</tr>
</table>
</td>
</tr>

</table>
</td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater></div>
     <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>

</form>
</body>
</html>



repeter.aspx.cs:-



using System;

using System.Data;

using System.Data.SqlClient;

using System.Web.UI.WebControls;
using System.Data.Linq;
using System.Collections;

public partial class repetar : System.Web.UI.Page
{
    private SqlConnection con = new SqlConnection("Data Source=SQLDB;Initial Catalog=Demo;User ID=Demoh;Password=Demo1@");
    protected void Page_Load(object sender, EventArgs e)
    {
      
        if (!IsPostBack)
        {
            bindcomment();
        }
    }
    // This button click event is used to insert comment details and bind data to repeater control
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        con.Open();
        SqlCommand cmd = new SqlCommand("insert into comment (unm,comment,postdate) values(@userName,@comment,@postedDate)", con);
        cmd.Parameters.AddWithValue("@userName", txtName.Text);

        cmd.Parameters.AddWithValue("@comment", txtComment.Text);
        cmd.Parameters.AddWithValue("@postedDate", DateTime.Now);
        cmd.ExecuteNonQuery();
        con.Close();
        txtName.Text = string.Empty;

        txtComment.Text = string.Empty;
        bindcomment();
    }
  
    //Bind Data to Repeater Control
    protected void bindcomment()
    {
         DataClasses2DataContext dc = new DataClasses2DataContext();
      
        SqlCommand cmd = new SqlCommand("select * from comment Order By postdate desc", con);
        DataTable dt = new DataTable();
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        da.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;
        }
            RepDetails.DataSource = pgitems;
            RepDetails.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;
        bindcomment();
    }

}
 


0 comments:

Post a Comment