Tuesday, August 27, 2013

Repeater Paging in C# without post back ("asynchronous paging")

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Repeater3.aspx.cs" Inherits="DUMMY_Repeater3" %>
<!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">   
    <asp:ScriptManager ID="MainScriptManager" runat="server" />
        <asp:UpdatePanel ID="pnlHelloWorld" runat="server">        
            <ContentTemplate>
                <div>
                    <asp:Repeater ID="rptItems" runat="server">        
                        <ItemTemplate>            
                            <div>
                                <div>
                                    <%# DataBinder.Eval(Container.DataItem,"Title") %>
                                </div>
                                <div>
                                    <%# DataBinder.Eval(Container.DataItem, "Message")%>
                                </div>
                                <div>
                                   <%-- <%# DataBinder.Eval(Container.DataItem, "BrandName")%>--%>
                                </div>
                            </div>
                        </ItemTemplate>
                    </asp:Repeater>
                </div>
                <div>
                <table>
                <tr>
                <td>
                <asp:LinkButton ID="lbPrev" runat="server" OnCommand="lbPrev_Command">Prev </asp:LinkButton> 
                </td>
                <td>
                <asp:Repeater ID="rptPages" runat="server">       
                        <ItemTemplate>
                            <div style="float:left;padding-left:10px;">
                                <asp:LinkButton ID="btnPage" style="padding:8px; margin:2px; background:#ffa100; border:solid 1px #666; font:8pt tahoma;" CommandName="Page" CommandArgument="<%#Container.DataItem %>" CssClass="text" runat="server"  ForeColor="White" Font-Bold="True"><%# Container.DataItem %>
                                </asp:LinkButton>
                            </div>
                        </ItemTemplate>
                    </asp:Repeater>
                </td>
                <td>
                 <asp:LinkButton ID="lbNext" runat="server" OnCommand="lbNext_Command">Next </asp:LinkButton> 
                </td>
                </tr>
                </table>
                   
                    
                  
                </div>
            </ContentTemplate>
        </asp:UpdatePanel>     
    </form>
</body>
</html>


 



using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.Collections;
using System.Configuration;
public partial class DUMMY_Repeater3 : System.Web.UI.Page
{
    public static int totalPages = 0;
    public int PageNumber
    {
        get
        {
            if (ViewState["PageNumber"] != null)
                return Convert.ToInt32(ViewState["PageNumber"]);
            else
                return 0;
        }
        set
        {
            ViewState["PageNumber"] = value;
        }
    }
    protected override void OnInit(EventArgs e)
    {
        base.OnInit(e);
        rptPages.ItemCommand += new RepeaterCommandEventHandler(rptPages_ItemCommand);
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
            LoadData();
    }
    private void LoadData()
    {
        SqlConnection cn = new SqlConnection(ConfigurationManager.ConnectionStrings["conStr"].ConnectionString.ToString());
        SqlCommand cmd = new SqlCommand("Select * from Announcement", cn);
        if (cn.State == ConnectionState.Closed)
        {
            cn.Open();
        }
        DataTable dt = new DataTable();
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        da.Fill(dt);
        cn.Close();
        PagedDataSource pgitems = new PagedDataSource();
        DataView dv = new DataView(dt);
        pgitems.DataSource = dv;
        pgitems.AllowPaging = true;
        pgitems.PageSize = 10;
        pgitems.CurrentPageIndex = PageNumber;
        totalPages = pgitems.PageCount - 1;
        if (pgitems.PageCount > 1)
        {
            rptPages.Visible = true;
            ArrayList pages = new ArrayList();
            for (int i = 0; i < pgitems.PageCount; i++)
                pages.Add((i + 1).ToString());
            rptPages.DataSource = pages;
            rptPages.DataBind();
        }
        else
            rptPages.Visible = false;
        rptItems.DataSource = pgitems;
        rptItems.DataBind();
    }
    void rptPages_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        PageNumber = Convert.ToInt32(e.CommandArgument) - 1;
        LoadData();
    }
    protected void lbPrev_Command(Object sender, CommandEventArgs e)
    {
        if (PageNumber == 0)
        {
            PageNumber = 0;
        }
        else
        {
            PageNumber = PageNumber - 1;
        }
        LoadData();
    }
    protected void lbNext_Command(Object sender, CommandEventArgs e)
    {
        if (PageNumber == totalPages)
        {
            PageNumber = totalPages;
        }
        else
        {
            PageNumber = PageNumber + 1;
        }
        LoadData();
    }
}

2 comments:

Unknown said...

I had a problem about it. And everything is great after your article.. Really thank you so much friend ! :)

simple money technique said...

you are welcome kemal.

Post a Comment