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.

Monday, February 25, 2013

Performing Edit Update and Delete in SharePoint List using GridView Command Fields within a UserControl




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="GridViewCommandFieldUserControl.ascx.cs" Inherits="GridViewCommandField.GridViewCommandField.GridViewCommandFieldUserControl" %>
<asp:GridView ID="dgvEUDSample" runat="server" Width="100%" BackColor="White"
            BorderColor="#336666" BorderStyle="Double" BorderWidth="3px" CellPadding="4"
            EnableModelValidation="True" AutoGenerateColumns="False"
    DataKeyNames="ID" onrowcancelingedit="dgvEUDSample_RowCancelingEdit"
    onrowdeleting="dgvEUDSample_RowDeleting" onrowediting="dgvEUDSample_RowEditing"
    onrowupdating="dgvEUDSample_RowUpdating">
        <FooterStyle BackColor="White" ForeColor="#333333" />
        <HeaderStyle BackColor="#336666" Font-Bold="True" ForeColor="White" />
        <PagerStyle BackColor="#336666" ForeColor="White" HorizontalAlign="Center" />
        <RowStyle BackColor="White" ForeColor="#333333" />
        <SelectedRowStyle BackColor="#339966" Font-Bold="True" ForeColor="White" />
        <Columns>
            <asp:TemplateField HeaderText="ItemID">
            <ItemTemplate> <%# Eval("ID") %></ItemTemplate>
            <AlternatingItemTemplate><%# Eval("ID") %></AlternatingItemTemplate>
            <EditItemTemplate><asp:Label ID="lblItemID" runat="server" Text=<%# Eval("ID") %>></asp:Label></EditItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Title">
            <ItemTemplate><%# Eval("Title") %></ItemTemplate>
            <AlternatingItemTemplate><%# Eval("Title") %></AlternatingItemTemplate>
            <EditItemTemplate><asp:TextBox ID="txtTitle" runat="server" Text=<%# Eval("Title") %>></asp:TextBox></EditItemTemplate>
            </asp:TemplateField>
            <asp:CommandField ShowEditButton="True" ButtonType="Button" ShowCancelButton="true" ShowDeleteButton="true" HeaderText="Edit, Update and Delete" />
        </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 GridViewCommandField.GridViewCommandField
{
    public partial class GridViewCommandFieldUserControl : UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                getData();
            }
        }

        public void getData()
        {
            SPSecurity.RunWithElevatedPrivileges(delegate()
            {
                SPListItemCollection myColl = SPContext.Current.Web.Lists["Articles"].Items;
                if (myColl.Count > 0)
                {
                    dgvEUDSample.DataSource = myColl.GetDataTable();
                    dgvEUDSample.DataBind();
                }
            });
        }

        protected void dgvEUDSample_RowEditing(object sender, GridViewEditEventArgs e)
        {
            try
            {
                dgvEUDSample.EditIndex = e.NewEditIndex;
                getData();

            }
            catch (Exception Ex)
            {
                Response.Write(Ex.ToString());
            }
        }

        protected void dgvEUDSample_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            try
            {
                Label lbl = (Label)dgvEUDSample.Rows[e.RowIndex].Cells[1].FindControl("lblItemID");
                TextBox txtArticles = (TextBox)dgvEUDSample.Rows[e.RowIndex].Cells[2].FindControl("txtTitle");
                updateRow(lbl.Text, txtArticles.Text);
                dgvEUDSample.EditIndex = -1;
                getData();
            }
            catch (Exception Ex)
            {
                Response.Write(Ex.ToString());
            }
        }

        public void updateRow(string strItemID, string strArticleName)
        {
            SPWeb currentWeb = SPContext.Current.Web;
            SPList lst = currentWeb.Lists["Articles"];
            SPListItem item = null;
            item = lst.GetItemById(int.Parse(strItemID));
            currentWeb.AllowUnsafeUpdates = true;
            item["Title"] = strArticleName;
            item.Update();
            lst.Update();
            currentWeb.AllowUnsafeUpdates = false;
        }

        public void DeleteRow(string strItemID)
        {
            SPWeb currentWeb = SPContext.Current.Web;
            SPList lst = currentWeb.Lists["Articles"];
            SPListItem item = null;
            item = lst.GetItemById(int.Parse(strItemID));
            currentWeb.AllowUnsafeUpdates = true;
            item.Delete();
            lst.Update();
            currentWeb.AllowUnsafeUpdates = false;

        }
        protected void dgvEUDSample_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            try
            {
                dgvEUDSample.EditIndex = -1;
                getData();
            }
            catch (Exception Ex)
            {
                Response.Write(Ex.ToString());
            }
        }

        protected void dgvEUDSample_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            string strItemID1 = dgvEUDSample.DataKeys[e.RowIndex].Value.ToString();
            DeleteRow(strItemID1);
            getData();

        }
    }
}

No comments:

Post a Comment