Prasad Bolla's SharePoint Blog

Click Here to go through the Interesting posts within my Blog.

Click Here to go through the new posts in my blog.

Friday, February 08, 2013

Custom Paging with Asp.Net Repeater Control using SharePoint Data




Ascx
<%@ Assembly Name="$SharePoint.Project.AssemblyFullName$" %>
<%@ Assembly Name="Microsoft.Web.CommandUI, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register Tagprefix="SharePoint" Namespace="Microsoft.SharePoint.WebControls" Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register Tagprefix="Utilities" Namespace="Microsoft.SharePoint.Utilities" Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register Tagprefix="asp" Namespace="System.Web.UI" Assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" %>
<%@ Import Namespace="Microsoft.SharePoint" %>
<%@ Register Tagprefix="WebPartPages" Namespace="Microsoft.SharePoint.WebPartPages" Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="RPTPagingUserControl.ascx.cs" Inherits="RPTPaging.RPTPaging.RPTPagingUserControl" %>
<style type="text/css">
.HeaderStyle
{
background-color:Navy;
color:white;
}
.ItemTemplateStyle
{
background-color:Orange;
color:white;
text-align:Center;
}
.AlternativeTemplateStyle
{
background-color:green;
color:white;
text-align:Center;
}
</style>
<asp:UpdatePanel ID="up1" runat="server">
<ContentTemplate>
<table width="100%">
<asp:Repeater ID="rrCustomPaging" runat="server">
<HeaderTemplate>
<tr>
<th class="HeaderStyle">Title</th>
<th class="HeaderStyle">Status</th>
<th class="HeaderStyle">Priority</th>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td class="ItemTemplateStyle">
<%# DataBinder.Eval(Container.DataItem, "Title") %>
</td>
<td class="ItemTemplateStyle">
<%# DataBinder.Eval(Container.DataItem, "Status") %>
</td>
<td class="ItemTemplateStyle">
<%# DataBinder.Eval(Container.DataItem, "Priority") %>
</td>
</tr>
</ItemTemplate>
<AlternatingItemTemplate>
<tr>
<td class="AlternativeTemplateStyle">
<%# DataBinder.Eval(Container.DataItem, "Title") %>
</td>
<td class="AlternativeTemplateStyle">
<%# DataBinder.Eval(Container.DataItem, "Status") %>
</td>
<td class="AlternativeTemplateStyle">
<%# DataBinder.Eval(Container.DataItem, "Priority") %>
</td>
</tr>
</AlternatingItemTemplate>
</asp:Repeater>
<asp:Repeater ID="rptPages" runat="server" onitemcommand="rptPages_ItemCommand">
                   <ItemTemplate>
                       <asp:LinkButton ID="btnPage"
                        style="padding:1px 3px; margin:1px; background:#ccc; border:solid 1px #666; font:8pt tahoma;"
                        CommandName="Page" CommandArgument="<%# Container.DataItem %>"
                        runat="server"><%# Container.DataItem %>
                       </asp:LinkButton>
                   </ItemTemplate>
               </asp:Repeater>
</table>
</ContentTemplate>
</asp:UpdatePanel>
Ascx.cs
using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using System.Security;
using System.Data;

namespace RPTPaging.RPTPaging
{
    public partial class RPTPagingUserControl : UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            getData();
        }
       
        public void getData()
        {
            PagedDataSource pgitems = new PagedDataSource();
            pgitems.DataSource = SPContext.Current.Web.Lists["Tasks"].Items.GetDataTable().DefaultView;
            pgitems.AllowPaging = true;
            pgitems.PageSize = 5;
            pgitems.CurrentPageIndex = PageNumber;
            rrCustomPaging.DataSource = pgitems;
            rrCustomPaging.DataBind();
            if (pgitems.PageCount > 1)
            {
                System.Collections.ArrayList pages = new System.Collections.ArrayList();
                for (int i = 0; i < pgitems.PageCount; i++)
                {
                    pages.Add((i + 1).ToString());
                    rptPages.DataSource = pages;
                    rptPages.DataBind();
                }
            }
        }
        public int PageNumber
        {
            get
            {
                if (ViewState["PageNumber"] != null)
                    return Convert.ToInt32(ViewState["PageNumber"]);
                else
                    return 0;
            }
            set
            {
                ViewState["PageNumber"] = value;
            }
        }

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


No comments:

Post a Comment