htmlcode:
----------
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="sample1.aspx.cs" Inherits="sample1" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<script type="text/javascript" language="jscript">
function validation() {
employeename = document.getElementById('<%=((TextBox)gridsample.FooterRow.FindControl("txtemployeename")).ClientID%>');
if (employeename.value == "") {
alert("please insert Employeename..");
employeename.focus();
return false;
}
employeecode = document.getElementById('<%=((TextBox)gridsample.FooterRow.FindControl("txtemployeecode")).ClientID%>');
if (employeecode.value == "") {
alert("please insert employeecode..");
employeecode.focus();
return false;
}
employeesalary = document.getElementById('<%=((TextBox)gridsample.FooterRow.FindControl("txtemployeesalary")).ClientID%>');
if (employeesalary.value == "") {
alert("please insert Employeesalary..");
employeesalary.focus();
return false;
}
gender = document.getElementById('<%=((RadioButtonList)gridsample.FooterRow.FindControl("gender")).ClientID%>');
if (gender.checked == false) {
var answer = confirm("Please confirm selection...")
if (answer)
return true;
else
return false;
}
department = document.getElementById('<%=((DropDownList)gridsample.FooterRow.FindControl("department")).ClientID%>');
if (department.value == "" || department.value == "---Select---") {
alert("please insert department..");
department.focus();
return false;
}
skills = document.getElementById('<%=((CheckBoxList)gridsample.FooterRow.FindControl("skills")).ClientID%>');
if (skills.value == "" || skills.value == "---Select---") {
alert("please insert skills..");
skills.focus();
return false;
}
project = document.getElementById('<%=((DropDownList)gridsample.FooterRow.FindControl("projects")).ClientID%>');
if (project.value == "" || project.value == "---Select---") {
alert("please insert project..");
project.focus();
return false;
}
}
function Isnumeric() {
var charcode = window.event.keyCode;
if (charcode > 31 && (charcode < 48 || charcode > 58)) {
return false;
}
else
return true;
}
function val(txtemployeename) {
txtBxObj = document.getElementById(txtemployeename);
if (txtBxObj.value == "") {
alert("pppppp");
return false;
}
}
</script>
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="gridsample" runat="server" AutoGenerateColumns="False" EmptyDataText="norecordsfound"
ShowHeaderWhenEmpty="true" ShowFooter="true" ShowHeader="true" Style="margin-right: 9px"
Width="1000px" OnRowCommand="gridsample_RowCommand" OnRowDataBound="gridsample_RowDataBound"
DataKeyNames="employeeno" OnRowCancelingEdit="gridsample_RowCancelingEdit" OnRowDeleting="gridsample_RowDeleting"
OnRowEditing="gridsample_RowEditing" OnRowUpdating="gridsample_RowUpdating">
<Columns>
<asp:TemplateField HeaderText="EMPLOYEE no.">
<ItemTemplate>
<asp:Label ID="lblemployeeno" runat="server" Text='<%# Eval("employeeno") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="EMPLOYEE NAME">
<EditItemTemplate>
<asp:TextBox ID="txtemployeename" runat="server" Text='<%# Eval ("employeename") %>'
Width="100px">
</asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblemployeename" runat="server" Text='<%# Eval ("employeename") %>'
Width="100px"></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtemployeename" runat="server"></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="EMPLOYEE Code">
<EditItemTemplate>
<asp:TextBox ID="txtemployeecode" runat="server" Text='<%# Eval ("employeecode") %>'
Width="100px">
</asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblemployeecode" runat="server" Text='<%# Eval ("employeecode") %>'
Width="100px"></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtemployeecode" runat="server" Text='<%# Eval ("employeecode") %>'
Width="100px"></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="EMPLOYEE Salary">
<EditItemTemplate>
<asp:TextBox ID="txtemployeesalary" runat="server" Text='<%# Eval ("salary") %>' onkeydown="return Isnumeric();"
Width="100px">
</asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblemployeesalary" runat="server" Text='<%# Eval ("salary") %>' Width="100px"></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtemployeesalary" runat="server" Text='<%# Eval ("salary") %>' onkeydown="return Isnumeric();"
Width="100px">
</asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Gender">
<EditItemTemplate>
<asp:RadioButtonList ID="gender" runat="server">
<asp:ListItem Value="Male" Text="Male"></asp:ListItem>
<asp:ListItem Value="Female" Text="Female"></asp:ListItem>
</asp:RadioButtonList>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblgender" runat="server" Text='<%# Eval("gender") %>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:RadioButtonList ID="gender" runat="server">
<asp:ListItem Value="Male" Text="Male" Selected="True"></asp:ListItem>
<asp:ListItem Value="Female" Text="Female"></asp:ListItem>
</asp:RadioButtonList>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Department">
<EditItemTemplate>
<asp:DropDownList ID="department" runat="server" Width="100px">
<asp:ListItem Text="---Select---"></asp:ListItem>
<asp:ListItem Value="dotnet" Text="dotnet"></asp:ListItem>
<asp:ListItem Value="java" Text="java"></asp:ListItem>
</asp:DropDownList>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lbldepartment" Text='<%# Eval("department") %>' runat="server"></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:DropDownList ID="department" runat="server" Width="100px">
<asp:ListItem Text="---Select---"></asp:ListItem>
<asp:ListItem Value="dotnet" Text="dotnet"></asp:ListItem>
<asp:ListItem Value="java" Text="java"></asp:ListItem>
</asp:DropDownList>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Skills">
<ItemTemplate>
<asp:Label ID="lblskills" Text='<%# Eval("skills") %>' runat="server"></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:CheckBoxList ID="skills" runat="server">
<asp:ListItem Value="asp.net" Text="Asp.Net"></asp:ListItem>
<asp:ListItem Value="c#.net" Text="C#.Net"></asp:ListItem>
<asp:ListItem Value="vb.net" Text="Vb.net"></asp:ListItem>
<asp:ListItem Value="oracle" Text="Oracle"></asp:ListItem>
<asp:ListItem Value="sqlserver" Text="SQLServer"></asp:ListItem>
<asp:ListItem Value="java" Text="Java"></asp:ListItem>
</asp:CheckBoxList>
</EditItemTemplate>
<FooterTemplate>
<asp:CheckBoxList ID="skills" runat="server">
<asp:ListItem Value="asp.net" Text="Asp.Net"></asp:ListItem>
<asp:ListItem Value="c#.net" Text="C#.Net"></asp:ListItem>
<asp:ListItem Value="vb.net" Text="Vb.net"></asp:ListItem>
<asp:ListItem Value="oracle" Text="Oracle"></asp:ListItem>
<asp:ListItem Value="sqlserver" Text="SQLServer"></asp:ListItem>
<asp:ListItem Value="java" Text="Java"></asp:ListItem>
</asp:CheckBoxList>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Projects">
<ItemTemplate>
<asp:Label ID="lblproject" Text='<%# Eval("projects") %>' runat="server"></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID="projects" runat="server" Width="100px">
<asp:ListItem Text="---Select---"></asp:ListItem>
<asp:ListItem Value="cambridge" Text="Cambridge"></asp:ListItem>
<asp:ListItem Value="SVBC" Text="SVBC"></asp:ListItem>
<asp:ListItem Value="HRMS" Text="HRMS"></asp:ListItem>
</asp:DropDownList>
</EditItemTemplate>
<FooterTemplate>
<asp:DropDownList ID="projects" runat="server" Width="100px">
<asp:ListItem Text="---Select---"></asp:ListItem>
<asp:ListItem Value="cambridge" Text="Cambridge"></asp:ListItem>
<asp:ListItem Value="SVBC" Text="SVBC"></asp:ListItem>
<asp:ListItem Value="HRMS" Text="HRMS"></asp:ListItem>
</asp:DropDownList>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField>
<EditItemTemplate>
<asp:LinkButton ID="linkupdate" runat="server" CausesValidation="true" CommandName="update"
Text="Update"></asp:LinkButton>
<asp:LinkButton ID="linkcancel" runat="server" CausesValidation="false" CommandName="cancel"
Text="Cancel"></asp:LinkButton>
</EditItemTemplate>
<ItemTemplate>
<asp:LinkButton ID="linkedit" runat="server" CausesValidation="true" CommandName="edit"
Text="Edit"></asp:LinkButton>
<asp:LinkButton ID="linkdelete" runat="server" CausesValidation="false" CommandName="delete"
OnClientClick="return confirm('Are you certain you want to delete this employee?');"
Text="Delete"></asp:LinkButton>
</ItemTemplate>
<FooterTemplate>
<asp:LinkButton ID="linkadd" runat="server" CommandName="newadd" Height="50px" Text="Add"
OnClientClick=" return validation()"></asp:LinkButton>
</FooterTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
<div>
<asp:Label ID="lblresult" runat="server"></asp:Label>
</div>
</form>
</body>
</html>
------------------------------------------------------------------------------------------------------------
code-back :
------------
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.Data;
namespace demo1
{
public partial class GridSample : System.Web.UI.Page
{
SqlConnection con = new SqlConnection("Data Source=WS-ECOM12\\SQLEXPRESS;Initial Catalog=samp;Integrated Security=True");
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
binddata();
}
}
protected void binddata()
{
SqlCommand cmd = new SqlCommand("Select * from newsamp", con);
con.Open();
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
con.Close();
if (ds.Tables[0].Rows.Count > 0)
{
gridsample.DataSource = ds;
gridsample.DataBind();
}
else
{
ds.Tables[0].Rows.Add(ds.Tables[0].NewRow());
gridsample.DataSource = ds;
gridsample.DataBind();
int columncount = gridsample.Rows[0].Cells.Count;
gridsample.Rows[0].Cells.Clear();
gridsample.Rows[0].Cells.Add(new TableCell());
gridsample.Rows[0].Cells[0].ColumnSpan = columncount;
//gridsample.Rows[0].Cells[0].Text = "No Records Found";
}
}
protected void gridsample_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName.Equals("newadd"))
{
TextBox txtemployeename = (TextBox)gridsample.FooterRow.FindControl("txtemployeename");
TextBox txtemployeecode = (TextBox)gridsample.FooterRow.FindControl("txtemployeecode");
TextBox txtemployeesalary = (TextBox)gridsample.FooterRow.FindControl("txtemployeesalary");
RadioButtonList gender = (RadioButtonList)gridsample.FooterRow.FindControl("gender");
DropDownList department = (DropDownList)gridsample.FooterRow.FindControl("department");
CheckBoxList skills = (CheckBoxList)gridsample.FooterRow.FindControl("skills");
ListBox projects = (ListBox)gridsample.FooterRow.FindControl("projects");
var values = "";
for (int i = 0; i < skills.Items.Count; i++)
{
if (skills.Items[i].Selected)
{
values += skills.Items[i].Value + ",";
}
}
values = values.TrimEnd(',');
SqlCommand cmd = new SqlCommand("insert into newsamp(employeename,employeecode,salary,gender,department,skills,projects)
values('" + txtemployeename.Text + "','" + txtemployeecode.Text + "','"
+ txtemployeesalary.Text + "','" + gender.SelectedValue + "','" +
department.SelectedValue + "','" + values + "','" +
projects.SelectedValue + "')", con);
con.Open();
int result = cmd.ExecuteNonQuery();
con.Close();
if (result == 1)
{
binddata();
string argScript = "alert('employee added successfully');";
ClientScript.RegisterStartupScript(typeof(Page), "PopUp", argScript, true);
}
else
{
string argScript = "alert('employee added unsuccessfully');";
ClientScript.RegisterStartupScript(typeof(Page), "PopUp", argScript, true);
}
}
}
protected void gridsample_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
TextBox txtemployeename = (TextBox)e.Row.FindControl("txtemployeename");
TextBox txtemployeecode = (TextBox)e.Row.FindControl("txtemployeecode");
TextBox txtemployeesalary = (TextBox)e.Row.FindControl("txtemployeesalary");
RadioButtonList gender = (RadioButtonList)e.Row.FindControl("gender");
DropDownList department = (DropDownList)e.Row.FindControl("department");
CheckBoxList skills1 = (CheckBoxList)e.Row.FindControl("skills");
ListBox projects = (ListBox)e.Row.FindControl("projects");
if (gender != null)
{
gender.SelectedValue = DataBinder.Eval(e.Row.DataItem, "gender").ToString();
}
if (department != null)
{
department.SelectedValue = DataBinder.Eval(e.Row.DataItem, "department").ToString();
}
if (projects != null)
{
projects.SelectedValue = DataBinder.Eval(e.Row.DataItem, "projects").ToString();
}
DataRow row = ((DataRowView)e.Row.DataItem).Row;
string skills = row.Field<String>("skills");
if (skills1 != null)
{
string[] strskill = skills.Split(',');
foreach (ListItem li in skills1.Items)
{
if (strskill.Contains(li.Value))
li.Selected = true;
else
li.Selected = false;
}
}
}
if (e.Row.RowType == DataControlRowType.Footer)
{
LinkButton add = (LinkButton)e.Row.FindControl("linkadd");
add.Attributes.Add("onclick", "adding();");
}
}
protected void gridsample_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
int employeeno = Convert.ToInt32(gridsample.DataKeys[e.RowIndex].Value.ToString());
TextBox txtemployeename = (TextBox)gridsample.Rows[e.RowIndex].FindControl("txtemployeename");
TextBox txtemployeecode = (TextBox)gridsample.Rows[e.RowIndex].FindControl("txtemployeecode");
TextBox txtemployeesalary = (TextBox)gridsample.Rows[e.RowIndex].FindControl("txtemployeesalary");
RadioButtonList gender = (RadioButtonList)gridsample.Rows[e.RowIndex].FindControl("gender");
DropDownList department = (DropDownList)gridsample.Rows[e.RowIndex].FindControl("department");
CheckBoxList skills = (CheckBoxList)gridsample.Rows[e.RowIndex].FindControl("skills");
ListBox projects = (ListBox)gridsample.Rows[e.RowIndex].FindControl("projects");
con.Open();
var values = "";
for (int i = 0; i < skills.Items.Count; i++)
{
if (skills.Items[i].Selected)
{
values += skills.Items[i].Value + ",";
}
}
values = values.TrimEnd(',');
SqlCommand cmd = new SqlCommand("update newsamp set
employeename='" + txtemployeename.Text + "', employeecode='" +
txtemployeecode.Text + "',salary='" + txtemployeesalary.Text +
"',gender='" + gender.SelectedValue + "',department='" +
department.SelectedValue + "',skills='" + values + "',projects='" +
projects.SelectedValue + "'where employeeno=" + employeeno, con);
cmd.ExecuteNonQuery();
con.Close();
gridsample.EditIndex = -1;
binddata();
string argScript = "alert('employee update successfully');";
ClientScript.RegisterStartupScript(typeof(Page), "PopUp", argScript, true);
}
protected void gridsample_RowEditing(object sender, GridViewEditEventArgs e)
{
gridsample.EditIndex = e.NewEditIndex;
binddata();
}
protected void gridsample_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
int employeeno = Convert.ToInt32(gridsample.DataKeys[e.RowIndex].Value.ToString());
con.Open();
SqlCommand cmd = new SqlCommand("delete from newsamp where employeeno=" + employeeno, con);
cmd.ExecuteNonQuery();
con.Close();
gridsample.EditIndex = -1;
binddata();
string argScript = "alert('employee deleted successfully');";
ClientScript.RegisterStartupScript(typeof(Page), "PopUp", argScript, true);
}
protected void gridsample_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
gridsample.EditIndex = -1;
binddata();
}
}
}