POC on gridView with Button element as Column value

To develop a code for the below requirement:
1)Create a GridView to display Employee table (first_name ,Last_name, Id)
2)For each row in the grid ,place a button "Exclude"inside the grid
3)On click of  the button "Exclude" put include the Emplyee name in a excluded list.
4)Write a function which can exclude and include the name of the employees from the Grid.
5)On submit button click show the number of Employess included and excluded.

ASPX Code

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Employee_Example.aspx.cs" Inherits="WebApplication1.Employee_Example" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
     <asp:GridView ID="employeeGrid" runat="server" CellPadding="4" ForeColor="#333333" GridLines="None" OnRowCommand="employeeGrid_viewCommand" OnRowDataBound="employeeGrid_RowDataBount">
         <AlternatingRowStyle BackColor="White" />
         <Columns>
             <asp:TemplateField>
                <ItemTemplate>
                <asp:Button ID="button_exclude" text="Exclude" runat="server" CommandName="Exclude" CommandArgument="<%# Container.DataItemIndex %>"/>
                </ItemTemplate>
             </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" />
         <SortedAscendingCellStyle BackColor="#F8FAFA" />
         <SortedAscendingHeaderStyle BackColor="#246B61" />
         <SortedDescendingCellStyle BackColor="#D4DFE1" />
         <SortedDescendingHeaderStyle BackColor="#15524A" />
        </asp:GridView>
    </div>
        <div>
            <asp:Label ID="Excluded" runat="server" Text="Excluded :"></asp:Label>
            <asp:Label ID="ListofExclude" runat="server" Text=""></asp:Label>
            <br/>
            <asp:Label ID="Included" runat="server" Text="Included :"></asp:Label>
            <asp:Label ID="ListofInclude" runat="server" Text=""></asp:Label>
            <br />
            <asp:Button ID="Button1" runat="server" Text="Submit" OnClick="Button1_Click"/>
        </div>
    </form>
</body>
</html>
 


CS Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Data.SqlClient;
using System.Text;

namespace WebApplication1
{
    public partial class Employee_Example : System.Web.UI.Page
    {

     
        List<string> name_exclude;
        List<string> name_include;

      
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                String Connection =System.Configuration.ConfigurationManager.ConnectionStrings["EmployeeConnection"].ConnectionString;
                SqlConnection conn= new SqlConnection(Connection);
                conn.Open();
                SqlCommand cmd =new SqlCommand("Select * from Employee",conn);
                SqlDataReader dr = cmd.ExecuteReader();
                employeeGrid.DataSource = dr;
                employeeGrid.DataBind();
                conn.Close();
            }


       

        }

        protected void employeeGrid_RowDataBount(Object sender ,GridViewRowEventArgs e)
        {
            if (this.ViewState["include"] == null)
            {
                this.ViewState["include"] = new List<string>();
            }

            var tring = (List<String>)ViewState["include"];
            tring.Add( e.Row.Cells[1].Text);
                 
        }



        protected void employeeGrid_viewCommand(Object sender, GridViewCommandEventArgs e)
        {
            int rowindex = Convert.ToInt32(e.CommandArgument);
            GridViewRow row = employeeGrid.Rows[rowindex];
            String name = row.Cells[1].Text;
            if (this.ViewState["excluded"] == null)
            {
                this.ViewState["excluded"] = new Dictionary<string,string>();
            }

            var status = (Dictionary<String, String>)ViewState["excluded"];
            if(!status.ContainsKey(name))
            {
                status.Add(name, "true");
            }
          
          


            if (e.CommandName == "Exclude" && status[name] == "true")
            {
               
             Button Button1 = (Button)e.CommandSource;
                if (Button1 != null)
                    Button1.Text = "Include";
                status[name] = "false";

                if (this.ViewState["exclude"] == null)
                {
                    this.ViewState["exclude"] = new List<string>();
                }

                var tring=(List<String>)ViewState["exclude"];
                tring.Add(name);
                var remove = (List<String>)ViewState["include"];
                remove.Remove(name);
            }

            else
            {
                Button Button1 = (Button)e.CommandSource;
                if (Button1 != null)
                    Button1.Text = "Exclude";

                status[name] = "true";

                if (this.ViewState["include"] == null)
                {
                    this.ViewState["include"] = new List<string>();
                }

                var tring = (List<String>)ViewState["include"];
                tring.Add(name);
                var remove = (List<String>)ViewState["exclude"];
                remove.Remove(name);
             


            }

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
             String  list="" ;
             String list1 = "";
            foreach (string name in (List<String>)ViewState["exclude"])
            {
                list = list +" , " +name;
            }
                ListofExclude.Text = list;
           

            foreach (string name in (List<String>)ViewState["include"])
            {
                list1 = list1 + " , " + name;
            }
            ListofInclude.Text = list1;
        }





        }

    }
 

DataBase Code:
create table Employee
(
first_name varchar(30),
last_name varchar(40),
Id int
)

insert into Employee values ('AMIT','MAZUDMER',201);
insert into Employee values ('BHARGAV','MAHANTA',202)
insert into Employee values ('JYOTISH','DAS',203)
insert into Employee values ('ARUN','SHARMA',204)
insert into Employee values ('SAMUDRA','BORA',205)
insert into Employee values ('JITEN','HAZARIKA',206)
insert into Employee values ('BULI','PHUKAN',207)
insert into Employee values ('JADAB','DAS',208)
insert into Employee values ('HIMANGA','LAHIRI',209)
insert into Employee values ('BIDYUT','DEB',210)
insert into Employee values ('MOMA','DUTTA',211)
insert into Employee values ('JYOTI','SAKIA',212)
insert into Employee values ('BHASKAR','DAS',213)
insert into Employee values ('RIMLI','DAS',214) 


SELECT * FROM EMPLOYEE

WebConfig  

  <connectionStrings>
    <add name="EmployeeConnection" providerName="System.Data.SqlClient" connectionString="Data Source=(LocalDB)\v11.0;AttachDbFilename=C:\USERS\APOTHEOSIS\DOCUMENTS\VISUAL STUDIO 2012\PROJECTS\1-CLICKPUBLISH\1-CLICKPUBLISH\APP_DATA\1-CLICKPUBLISH.MDF;Integrated Security=True;Connect Timeout=30"/>
  </connectionStrings> 

Comments

Popular posts from this blog

Authentication and Authorization in Web API -Part1

My Gardening Journey 6