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

Using ViewState to increase the Performance in Asp.Net GridVIew while retrieving 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="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%"
    CellPadding="4" EnableModelValidation="True" ForeColor="#333333"
    GridLines="Both" AllowPaging="True"  PageSize="20"
        onpageindexchanging="sgvCountries_PageIndexChanging">
    <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" />
    <PagerSettings Mode="NextPrevious" NextPageText="Next"
        PreviousPageText="Previous" />
    <PagerStyle BackColor="#666666" ForeColor="White" HorizontalAlign="Center" />
    <RowStyle BackColor="#E3EAEB" />
    <SelectedRowStyle BackColor="#C5BBAF" Font-Bold="True" ForeColor="#333333" />
</asp:GridView>
</td>
</tr>
</table>
<asp:Label ID="lblMessage" runat="server"></asp:Label>
</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;
using System.Linq;
using System.Collections.Generic;

namespace AjaxTestingNew.AjaxTestingNew
{
    public partial class AjaxTestingNewUserControl : UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                AjaxTestingNewUserControl obj = new AjaxTestingNewUserControl();
                ViewState["SharePointData"] = obj.getData();
                sgvCountries.DataSource = ViewState["SharePointData"];
                sgvCountries.DataBind();
            }
        }

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

        protected void sgvCountries_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            if (ViewState["SharePointData"] != null)
            {
                sgvCountries.DataSource = ViewState["SharePointData"];
                sgvCountries.PageIndex = e.NewPageIndex;
                sgvCountries.DataBind();
            }
        }
   }
}

No comments:

Post a Comment