Search This Blog

Saturday, September 5, 2015

Insertion In GridView By Footer in List



1- Create List Manually  Contacts -- FName,City,TelNo



<%@ 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="InsertionInGridByFooterUserControl.ascx.cs" Inherits="InsertionInGridByFooter.InsertionInGridByFooter.InsertionInGridByFooterUserControl" %>
<asp:GridView ID="dgvInsertRecord" runat="server" AutoGenerateColumns="False" Width="600px" EmptyDataText="No Records Of Contact Numbers"
    CellPadding="4" ForeColor="#333333" GridLines="None"  ShowFooter="True"
    onrowcommand="dgvInsertRecord_RowCommand">
        <Columns>
            <asp:TemplateField>
            <HeaderTemplate>
            <table width="100%" cellpadding="0" cellspacing="0">
            <tr>
            <td width="25%">
            Fname
            </td>
             <td width="25%">
            City
            </td>
             <td width="50%">
           Telno
            </td>
            </tr>
            </table>
            </HeaderTemplate>
            <ItemTemplate >
            <table width="100%" cellpadding="0" cellspacing="0">
            <tr>
            <td align="center" width="25%">
            <%#Eval("Title") %>
            </td>
              <td align="center" width="25%">
             <%#Eval("City") %>
            </td>
             <td align="center" width="50%">
            <%#Eval("Telno") %>
            </td>
            </tr>
            </table>
            </ItemTemplate>
            <FooterTemplate>
            <table  cellpadding="0" cellspacing="0">
            <tr>
            <td align="center" width="25%">
            <asp:TextBox ID="txtFname" runat="server"></asp:TextBox>
            </td>
            <td align="center" width="25%">
            <asp:TextBox ID="txtCity" runat="server"></asp:TextBox>
            </td>
            <td align="center" width="50%">
            <asp:TextBox ID="txtTelno" runat="server"></asp:TextBox>&nbsp;<asp:Button ID="btnSave" runat="server" Text="Save" CommandName="Insert" />
            </td>
            </tr>
            </table>
            </FooterTemplate>
            </asp:TemplateField>
        </Columns>
            <RowStyle BackColor="#E3EAEB" />
            <FooterStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
            <PagerStyle BackColor="#666666" ForeColor="White" HorizontalAlign="Center" />
            <SelectedRowStyle BackColor="#C5BBAF" Font-Bold="True" ForeColor="#333333" />
            <HeaderStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
            <EditRowStyle BackColor="#7C6F57" />
            <AlternatingRowStyle BackColor="White" />
        </asp:GridView>



using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using System.Security;
namespace InsertionInGridByFooter.InsertionInGridByFooter
{
    public partial class InsertionInGridByFooterUserControl : UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindData();
            }
        }

        public void BindData()
        {
            SPWeb currentWeb = SPContext.Current.Web;
            SPList lst = currentWeb.Lists["Contacts"];
            SPListItemCollection myColl = lst.Items;
            if (lst.Items.Count > 0)
            {
                dgvInsertRecord.DataSource = myColl.GetDataTable();
                dgvInsertRecord.DataBind();
            }
            else
            {
                //dgvInsertRecord.DataSource = myColl.GetDataTable();
                //dgvInsertRecord.DataBind();

            }

          
        }

        protected void dgvInsertRecord_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "Insert")
            {
                SPWeb currentWeb = SPContext.Current.Web;
                SPList lst = currentWeb.Lists["Contacts"];
                SPListItemCollection myColl = lst.Items;
                TextBox txtFname1 = (TextBox)dgvInsertRecord.FooterRow.FindControl("txtFname");
                TextBox txtCity1 = (TextBox)dgvInsertRecord.FooterRow.FindControl("txtCity");
                TextBox txtTelno1 = (TextBox)dgvInsertRecord.FooterRow.FindControl("txtTelno");
                SPListItem item = myColl.Add();
                item["Title"] = txtFname1.Text;
                item["City"] = txtCity1.Text;
                item["Telno"] = txtTelno1.Text;
                item.Update();
                txtFname1.Text = "";
                txtCity1.Text = "";
                txtTelno1.Text = "";
                BindData();
            }
        }
    }
}




No comments:

Post a Comment