Note:-
- For Enabling SessionState in SharePoint 2010 I have followed the below link. http://www.kajanmoorthy.com/2010/05/enable-session-state-in-sharepoint-2010.html
- No Need to Use Session State or View State for this Example as you can need to touch DataBase while updation & Deletion. I just Tried show how to use session in SharePoint 2010.
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="COMIEUDGVUserControl.ascx.cs" Inherits="COMIEUDGV.COMIEUDGV.COMIEUDGVUserControl"
%>
<asp:GridView ID="dgvCOMSamples" runat="server" AutoGenerateColumns="False"
DataKeyNames="ID" Width="100%" CellPadding="4" EnableModelValidation="True"
ForeColor="#333333" GridLines="Both" AutoGenerateDeleteButton="True"
AutoGenerateEditButton="True"
ShowFooter="true"
onrowcancelingedit="dgvCOMSamples_RowCancelingEdit"
onrowdeleting="dgvCOMSamples_RowDeleting"
onrowediting="dgvCOMSamples_RowEditing"
onrowupdating="dgvCOMSamples_RowUpdating"
onrowcommand="dgvCOMSamples_RowCommand">
<AlternatingRowStyle BackColor="White" />
<Columns>
<asp:TemplateField HeaderText="ItemID">
<ItemTemplate>
<%# Eval("ID") %>
</ItemTemplate>
<EditItemTemplate>
<asp:Label ID="lblItemID" runat="server" Text='<%# Eval("ID") %>' Columns="3" />
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Title">
<ItemTemplate>
<%# Eval("Title") %>
</ItemTemplate>
<FooterTemplate>
<table width="100%" cellpadding="0" cellspacing="0">
<tr>
<td><asp:TextBox ID="txtArticles" runat="server"></asp:TextBox> <asp:Button ID="btnSave"
runat="server"
Text="Save"
CommandName="Insert"
/></td>
</tr>
</table>
</FooterTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtTitle" runat="server" Text='<%# Eval("Title")
%>' Columns="3" Width="25%" />
</EditItemTemplate>
</asp:TemplateField>
</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>
<asp:Label ID="lblMessage" runat="server"></asp:Label>
|
Ascx.cs
|
using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using
System.Web.UI.WebControls.WebParts;
using System.Collections.Generic;
using Microsoft.SharePoint;
using SP=Microsoft.SharePoint.Client;
namespace COMIEUDGV.COMIEUDGV
{
public partial class COMIEUDGVUserControl : UserControl
{
protected
void Page_Load(object
sender, EventArgs e)
{
if
(!IsPostBack)
{
Session["ArticlesData"] = getData();
dgvCOMSamples.DataSource =
Session["ArticlesData"];
dgvCOMSamples.DataBind();
}
}
[Serializable]
public
class Articles
{
public
string Title { get;
set; }
public
string ID { get;
set; }
}
private
void BindData()
{
if
(Session["ArticlesData"] != null)
{
Session["ArticlesData"] = getData();
dgvCOMSamples.DataSource =
Session["ArticlesData"];
dgvCOMSamples.DataBind();
}
}
public
List<Articles>
getData()
{
List<Articles> ArticlesData = new List<Articles>();
using
(SP.ClientContext clientContext = new SP.ClientContext(SPContext.Current.Web.Url))
{
SP.List
list = clientContext.Web.Lists.GetByTitle("Articles");
var
camlQuery = new SP.CamlQuery
{ ViewXml = "<View/>" };
SP.ListItemCollection
listItems = list.GetItems(camlQuery);
clientContext.Load(listItems);
clientContext.ExecuteQuery();
foreach
(SP.ListItem item in
listItems)
{
ArticlesData.Add(new Articles()
{
ID = item.Id.ToString(),
Title = (item["Title"] == null)
? "Data UnAvailable" : item["Title"].ToString()
});
}
}
return
ArticlesData;
}
protected
void dgvCOMSamples_RowEditing(object sender, GridViewEditEventArgs
e)
{
dgvCOMSamples.EditIndex =
e.NewEditIndex;
BindData();
}
protected
void dgvCOMSamples_RowUpdating(object sender, GridViewUpdateEventArgs
e)
{
Label
lbl = (Label)dgvCOMSamples.Rows[e.RowIndex].Cells[2].FindControl("lblItemID");
TextBox
txtTitle1 = (TextBox)dgvCOMSamples.Rows[e.RowIndex].Cells[2].FindControl("txtTitle");
updateRow(lbl.Text,
txtTitle1.Text);
dgvCOMSamples.EditIndex = -1;
BindData();
lblMessage.Text = "Record Updated Successfully";
}
public
void updateRow(string
ItemID1, string Title)
{
SP.ClientContext
clientContext = new SP.ClientContext(SPContext.Current.Web.Url);
SP.Web
web = clientContext.Web;
SP.List
list = web.Lists.GetByTitle("Articles");
SP.CamlQuery
query = new SP.CamlQuery();
query.ViewXml = @"<Query><Where><Eq><FieldRef
Name='ID' /><Value Type='Counter'>" + ItemID1 + "</Value></Eq></Where></Query>";
SP.ListItemCollection
listItems = list.GetItems(query);
clientContext.Load(listItems);
clientContext.ExecuteQuery();
foreach
(Microsoft.SharePoint.Client.ListItem item
in listItems)
{
if
(ItemID1 == item["ID"].ToString())
{
item["Title"] = Title;
item.Update();
break;
}
}
clientContext.ExecuteQuery();
}
public
void DeleteRow(string
ItemIDNew)
{
SP.ClientContext
clientContext = new SP.ClientContext(SPContext.Current.Web.Url);
SP.Web
web = clientContext.Web;
SP.List
list = web.Lists.GetByTitle("Articles");
SP.CamlQuery
query = new SP.CamlQuery();
query.ViewXml = @"<Query><Where><Eq><FieldRef
Name='ID' /><ValueType='Counter'>" + ItemIDNew + "</Value></Eq></Where></Query>";
SP.ListItemCollection
listItems =list.GetItems(query);
clientContext.Load(listItems);
clientContext.ExecuteQuery();
foreach
(SP.ListItem item in
listItems)
{
if
(ItemIDNew == item["ID"].ToString())
{
item.DeleteObject();
break;
}
}
clientContext.ExecuteQuery();
}
protected
void dgvCOMSamples_RowCancelingEdit(object sender, GridViewCancelEditEventArgs
e)
{
dgvCOMSamples.EditIndex = -1;
BindData();
}
string
strItemID1;
protected
void dgvCOMSamples_RowDeleting(object sender, GridViewDeleteEventArgs
e)
{
strItemID1 =
dgvCOMSamples.DataKeys[e.RowIndex].Value.ToString();
DeleteRow(strItemID1);
BindData();
lblMessage.Text = "Record Deleted Successfully";
}
protected
void dgvCOMSamples_RowCommand(object sender, GridViewCommandEventArgs
e)
{
if
(e.CommandName == "Insert")
{
SP.ClientContext
ctx = new SP.ClientContext(SPContext.Current.Web.Url);
SP.List
ArticlesList = ctx.Web.Lists.GetByTitle("Articles");
SP.ListItemCreationInformation
newRecord = new SP.ListItemCreationInformation();
Microsoft.SharePoint.Client.ListItem item = ArticlesList.AddItem(newRecord);
TextBox
txtNewArticle = (TextBox)dgvCOMSamples.FooterRow.FindControl("txtArticles");
item["Title"]
= txtNewArticle.Text;
item.Update();
ctx.ExecuteQuery();
txtNewArticle.Text = "";
BindData();
lblMessage.Text = "New Record Inserted";
}
}
}
}
|
• Nice and good article. It is very useful for me to learn and understand easily. Thanks for sharing your valuable information and time. Please keep updatingAzure Online Training bangalore
ReplyDeleteperde modelleri
ReplyDeletesms onay
Mobil Ödeme Bozdurma
nft nasil alinir
Ankara Evden Eve Nakliyat
trafik sigortası
dedektör
Site kurmak
Ask kitaplari
özel ambulans
ReplyDeletenft nasıl alınır
en son çıkan perde modelleri
minecraft premium
lisans satın al
yurtdışı kargo
uc satın al
en son çıkan perde modelleri