Search This Blog

Saturday, September 27, 2014

Paging without Refresh in GridView using Visual WebPart

Note:-
  1. Here we are fetching data from SharePoint List.
  2. We have to keep the GridView in UpdatePanel.



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="AjaxTestingNewUserControl.ascx.cs"
Inherits="AjaxTestingNew.AjaxTestingNew.AjaxTestingNewUserControl"  %>
 <%@ Register Assembly="AjaxControlToolkit, Version=3.0.30930.28736, 
Culture=neutral, PublicKeyToken=28f01b0e84b6d53e" Namespace="AjaxControlToolkit"
TagPrefix="AjaxControlToolkit" %>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<table width="100%" cellpadding="0" cellspacing="0">
<tr>
<td>
<asp:GridView ID="sgvCountries" runat="server" AutoGenerateColumns="False"
    Width="100%"  AllowPaging="True" PageSize="10"
    CellPadding="4" EnableModelValidation="True" ForeColor="#333333"
    GridLines="Both">
    <AlternatingRowStyle BackColor="White" />
<Columns>
<asp:BoundField HeaderText="Country" DataField="Title" />
<asp:BoundField HeaderText="Region" DataField="Region" />
</Columns>
    <EditRowStyle BackColor="#7C6F57" />
    <FooterStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
    <HeaderStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
    <PagerStyle BackColor="#666666" ForeColor="White" HorizontalAlign="Center"/>
    <RowStyle BackColor="#E3EAEB" />
    <SelectedRowStyle BackColor="#C5BBAF" Font-Bold="True" ForeColor="#333333"/>
</asp:GridView>
</td>
</tr>
</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 AjaxTestingNew.AjaxTestingNew
{
    public partial class AjaxTestingNewUserControl : UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            sgvCountries.PagerSettings.Mode = (PagerButtons)PagerMode.NextPrev;
            sgvCountries.PagerSettings.NextPageText = "Next ";
            sgvCountries.PagerSettings.PreviousPageText = "Previous";
            sgvCountries.PageIndexChanging += newGridViewPageEventHandler(sgvCountries_PageIndexChanging);
            if (!IsPostBack)
            {
                sgvCountries.DataSource = BindGridView();
                sgvCountries.DataBind();
            }
           
        }

        void sgvCountries_PageIndexChanging(object sender, GridViewPageEventArgse)
        {
            sgvCountries.PageIndex = e.NewPageIndex;
            sgvCountries.DataSource = BindGridView();
            sgvCountries.DataBind();
        }

        public DataTable BindGridView()
        {
            SPWeb currentWeb = SPContext.Current.Web;
            SPList lstEmployee = currentWeb.Lists["lst_Country"];
            SPQuery sQuery = new SPQuery();
            sQuery.Query = "<OrderBy><FieldRef Name='ID' Ascending='False' /></OrderBy>";
            SPListItemCollection myColl = lstEmployee.GetItems(sQuery);
            return myColl.GetDataTable();
        }
   
    }
}


No comments:

Post a Comment