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.

Wednesday, December 12, 2012

GridView Dynamic Paging Sample in SharePoint

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="VisualWebPart1UserControl.ascx.cs" Inherits="dgvHTMLBinding.VisualWebPart1.VisualWebPart1UserControl" %>
<style type="text/css">
.HeaderStyle
{
background-color:Navy;
color:white;
}
.ItemTemplateStyle
{
background-color:Orange;
color:white;
padding-left:5px;
/*text-align:Center;*/
}
.AlternativeTemplateStyle
{
background-color:green;
color:white;
padding-left:5px;
/*text-align:Center;*/
}
.BlankTr
{
    background-color:Red;
}
</style>
<asp:GridView ID="dgvBreakingNews" runat="server" AutoGenerateColumns="false" Width="100%" CellPadding="0" CellSpacing="0">
<Columns>
<asp:TemplateField>
<HeaderTemplate>
<div class="BlankTr" style="width:100%;height:2px;">&nbsp;</div>
<div style="width:100%;text-align:center;" class="HeaderStyle">
Breaking News
</div>
<div class="BlankTr" style="width:100%;height:2px;">&nbsp;</div>
</HeaderTemplate>
<ItemTemplate>
<div style="width:100%;" class="ItemTemplateStyle">
<span>
<%# Eval("ID") %>.
</span>
<span>
<strong><%# Eval("Title") %></strong>
</span>
</div>
<div class="BlankTr" style="width:100%;height:2px;">&nbsp;</div>
</ItemTemplate>
<AlternatingItemTemplate>
<div style="width:100%;" class="AlternativeTemplateStyle">
<span>
<%# Eval("ID") %>.
</span>
<span>
<strong><%# Eval("Title") %></strong>
</span>
</div>
<div class="BlankTr" style="width:100%;height:2px;">&nbsp;</div>
</AlternatingItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>

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;

namespace dgvHTMLBinding.VisualWebPart1
{
    public partial class VisualWebPart1UserControl : UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            dgvBreakingNews.AllowPaging = true;
            int k = getPageIndexValueDynamically();
            dgvBreakingNews.PageSize = k;
            dgvBreakingNews.PagerSettings.Mode = (PagerButtons)PagerMode.NextPrev;
            dgvBreakingNews.PagerSettings.NextPageText = "Next ";
            dgvBreakingNews.PagerSettings.PreviousPageText = "Previous";
            dgvBreakingNews.PageIndexChanging += new GridViewPageEventHandler(dgvBreakingNews_PageIndexChanging);
            if (!IsPostBack)
            {
                BindGridVIew();
            }

        }

        void dgvBreakingNews_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            dgvBreakingNews.PageIndex = e.NewPageIndex;
            BindGridVIew();
        }

        public void BindGridVIew()
        {
            SPWeb currentWeb = SPContext.Current.Web;
            SPList lst = currentWeb.Lists["Breaking News"];
            SPQuery sQuery = new SPQuery();
            sQuery.Query = "<OrderBy><FieldRef Name='ID' Ascending='False' /></OrderBy>";
            SPListItemCollection myColl = lst.GetItems(sQuery);
            dgvBreakingNews.DataSource = myColl.GetDataTable();
            dgvBreakingNews.DataBind();
        }
        public int getPageIndexValueDynamically()
        {
            int i;
            SPWeb currentWeb = SPContext.Current.Web;
            SPList lst = currentWeb.Lists["GridView Page Count"];
            SPListItemCollection myColl = lst.Items;
            SPListItem item = myColl[0];
            i = int.Parse(item.Title);
            return i;
        }
    }
}

No comments:

Post a Comment