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();
        }
   
    }
}


How to Use Three Tier Architecture in SharePoint 2013 Visual WebPart

 SharePoint 2013 List Back end

Ascx Page 
<%@ Assembly Name="$SharePoint.Project.AssemblyFullName$" %>
<%@ Assembly Name="Microsoft.Web.CommandUI, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register Tagprefix="SharePoint" Namespace="Microsoft.SharePoint.WebControls"Assembly="Microsoft.SharePoint, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register Tagprefix="Utilities" Namespace="Microsoft.SharePoint.Utilities"Assembly="Microsoft.SharePoint, Version=15.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=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="ThreeLayesr.ascx.cs"Inherits="ThreeLayesr.ThreeLayesr.ThreeLayesr" %>
<table cellpadding="0" cellspacing="0" align="center">
    <tr><td colspan="2" align="center"></td></tr>
    <tr><td>Enter UserName</td><td>
        <asp:TextBox ID="txtUserName" runat="server"></asp:TextBox>
        </td></tr>
    <tr><td>Enter PassWord</td><td>
        <asp:TextBox ID="txtPassWord" runat="server"></asp:TextBox>
        </td></tr>
    <tr><td colspan="2" align="center">
        <asp:Button ID="btnSave" runat="server" Text="Save Record" OnClick="btnSave_Click" />
        </td></tr>
</table>

  Ascx.Cs
using System;
using System.ComponentModel;
using System.Web.UI.WebControls.WebParts;

namespace ThreeLayesr.ThreeLayesr
{
    [ToolboxItemAttribute(false)]
    public partial class ThreeLayesr : WebPart
    {
        // Uncomment the following SecurityPermission attribute only when doing Performance Profiling on a farm solution
        // using the Instrumentation method, and then remove the SecurityPermission attribute when the code is ready
        // for production. Because the SecurityPermission attribute bypasses the security check for callers of
        // your constructor, it's not recommended for production purposes.
        // [System.Security.Permissions.SecurityPermission(System.Security.Permissions.SecurityAction.Assert, UnmanagedCode = true)]
        DataBase db = new DataBase();
        public ThreeLayesr()
        {
        }

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            InitializeControl();
        }

        protected void Page_Load(object sender, EventArgs e)
        {
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            EntityClass obj=new EntityClass();
            obj.strUserName = txtUserName.Text;
            obj.strPassWord = txtPassWord.Text;
            db.InsertSample(obj.strUserName, obj.strPassWord);
            Page.Response.Write("Insertion Successful");
        }
    }
}

Business Layer CS File

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


namespace ThreeLayesr
{
    public class EntityClass
    {
        string UserName;
        string PassWord;

        public string strUserName
        {
            get
            {
                return UserName;
            }

            set
            {
                UserName = value;
            }
        }

        public string strPassWord
        {
            get
            {
                return PassWord;
            }

            set
            {
                PassWord = value;
            }
        }
    }
}

Database Layer CS File
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.SharePoint;
using System.Security;

namespace ThreeLayesr
{
    public class DataBase
    {
       
        public void InsertSample(string strUserName, string strPassWord)
        {
            SPListItemCollection myColl = SPContext.Current.Web.Lists["Login"].Items;
            SPListItem item = myColl.Add();
            item["Title"] = strUserName;
            item["PassWord"] = strPassWord;
            item.Update();
        }
    }
}