View code :
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Admin.aspx.cs" Inherits="WebApplication9.Admin" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<table align="center" cellpadding="1" cellspacing="1" border="0" style="width: 800px">
<tr>
<td></td>
<td>
<asp:Label ID="lbmess" runat="server" ForeColor="Green"></asp:Label>
<td></td>
</tr>
<tr>
<td>EID</td>
<td>
<asp:TextBox ID="txteid" runat="server" Width="200"></asp:TextBox></td>
<td>
<asp:RequiredFieldValidator ID="reid" runat="server" ControlToValidate="txteid" ErrorMessage="EID should not be blank."></asp:RequiredFieldValidator></td>
</tr>
<tr>
<td>NAME</td>
<td>
<asp:TextBox ID="txtname" runat="server" Width="200"></asp:TextBox></td>
<td>
<asp:RequiredFieldValidator ID="rname" runat="server" ControlToValidate="txtname" ErrorMessage="Name should not be blank."></asp:RequiredFieldValidator></td>
</tr>
<tr>
<td>ADDRESS</td>
<td>
<asp:TextBox ID="txtadd" Width="200" runat="server" TextMode="MultiLine"></asp:TextBox></td>
<td>
<asp:RequiredFieldValidator ID="txtadd1" runat="server" ControlToValidate="txtadd" ErrorMessage="Address should not be blank."></asp:RequiredFieldValidator></td>
</tr>
<tr>
<td>PASSWORD</td>
<td>
<asp:TextBox ID="txtpwd" Width="200" MaxLength="8" runat="server" TextMode="Password"></asp:TextBox></td>
<td>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="txtpwd" ErrorMessage="Password should not be blank."></asp:RequiredFieldValidator></td>
</tr>
<tr>
<td style="width: 300px">CONFIRM PASSWORD</td>
<td>
<asp:TextBox ID="txtcpwd" Width="200" MaxLength="8" runat="server" TextMode="Password"></asp:TextBox></td>
<td>
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ControlToValidate="txtcpwd" ErrorMessage="Confirm password should not be blank."></asp:RequiredFieldValidator><asp:CompareValidator ID="pcom" runat="server" ErrorMessage="Password and confirm password must be same." ControlToCompare="txtpwd" ControlToValidate="txtcpwd"></asp:CompareValidator></td>
</tr>
<tr>
<td>GENDER</td>
<td>
<asp:RadioButtonList runat="server" ID="rb" Width="200">
</asp:RadioButtonList>
</td>
<td>
<asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" ControlToValidate="rb" ErrorMessage="Please select a gender."></asp:RequiredFieldValidator></td>
</tr>
<tr>
<td>HOBBY</td>
<td>
<asp:CheckBoxList ID="cbl" runat="server" Width="200">
</asp:CheckBoxList></td>
<td>
<asp:CustomValidator ID="cv" runat="server" ErrorMessage="Please select a hobby." ClientValidationFunction="ValidateCheckBoxList">
</asp:CustomValidator>
</td>
</tr>
<tr>
<td>SALARY</td>
<td>
<asp:TextBox ID="txtsal" Width="200" runat="server"></asp:TextBox></td>
<td>
<asp:RequiredFieldValidator ID="RequiredFieldValidator5" runat="server" ControlToValidate="txtsal" ErrorMessage="Salary should not be blank."></asp:RequiredFieldValidator><asp:RangeValidator ControlToValidate="txtsal" runat="server" ID="rnv" MinimumValue="5000" MaximumValue="500000" ErrorMessage="Salary range between 5000 to 500000" Type="Integer"></asp:RangeValidator></td>
</tr>
<tr>
<td>EMAIL</td>
<td>
<asp:TextBox ID="txtemail" Width="200" runat="server"></asp:TextBox></td>
<td>
<asp:RequiredFieldValidator ID="RequiredFieldValidator6" runat="server" ControlToValidate="txtemail" ErrorMessage="Email should not be blank.">
</asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ID="rev" runat="server" ControlToValidate="txtemail" ErrorMessage="Invalid Email id" ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"></asp:RegularExpressionValidator>
</td>
</tr>
<tr>
<td>INTEREST</td>
<td>
<asp:ListBox ID="lb" runat="server" Width="200" SelectionMode="Multiple"></asp:ListBox>
</td>
<td>
<asp:RequiredFieldValidator ID="RequiredFieldValidator7" runat="server" ControlToValidate="lb" ErrorMessage="Please select a interest."></asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td>COUNTRY</td>
<td>
<asp:DropDownList ID="ddl" runat="server" Width="200" AutoPostBack="True" OnSelectedIndexChanged="ddl_SelectedIndexChanged"></asp:DropDownList>
</td>
<td>
<asp:RequiredFieldValidator ID="RequiredFieldValidator8" runat="server" ControlToValidate="ddl" ErrorMessage="Please select a country."></asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td>STATE</td>
<td>
<asp:DropDownList ID="ddl1" runat="server" Width="200"></asp:DropDownList>
</td>
<td>
<asp:RequiredFieldValidator ID="RequiredFieldValidator9" runat="server" ControlToValidate="ddl1" ErrorMessage="Please select a state."></asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td> </td>
<td>
<asp:Button ID="btn" runat="server" Text="Submit" Width="80" OnClick="btn_Click" />
<asp:Button ID="btn1" runat="server" Text="Reset" Width="80" OnClick="btn1_Click" />
</td>
<td>
</td>
</tr>
<tr>
<td colspan="3">
<asp:GridView ID="gv" runat="server" AutoGenerateColumns="False" AllowPaging="True" CellPadding="4" ForeColor="#333333" GridLines="None" OnPageIndexChanging="gv_PageIndexChanging" OnRowDeleting="gv_RowDeleting" OnSelectedIndexChanging="gv_SelectedIndexChanging" PageSize="2" Width="800px">
<AlternatingRowStyle BackColor="White" />
<Columns>
<asp:BoundField DataField="EID" HeaderText="EID" />
<asp:BoundField DataField="NAME" HeaderText="NAME" />
<asp:BoundField DataField="GENDER" HeaderText="GENDER" />
<asp:CommandField ShowSelectButton="true" HeaderText="UPDATE" />
<asp:CommandField ShowDeleteButton="true" HeaderText="DELETE" />
</Columns>
<EditRowStyle BackColor="#2461BF" />
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#EFF3FB" />
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#F5F7FB" />
<SortedAscendingHeaderStyle BackColor="#6D95E1" />
<SortedDescendingCellStyle BackColor="#E9EBEF" />
<SortedDescendingHeaderStyle BackColor="#4870BE" />
</asp:GridView>
</td>
</tr>
</table>
</div>
</form>
<script type="text/javascript">
function ValidateCheckBoxList(sender, args) {
var checkBoxList = document.getElementById("<%=cbl.ClientID %>");
var checkboxes = checkBoxList.getElementsByTagName("input");
var isValid = false;
for (var i = 0; i < checkboxes.length; i++) {
if (checkboxes[i].checked) {
isValid = true;
break;
}
}
args.IsValid = isValid;
}
</script>
</body>
</html>
C# code :
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace WebApplication9
{
public partial class Admin : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
CLR();
fill();
fillgv();
}else
{
txtpwd.Attributes["value"] = txtpwd.Text;
txtcpwd.Attributes["value"] = txtcpwd.Text;
}
}
public void fill()
{
using (Database1Entities obj = new WebApplication9.Database1Entities())
{
cbl.Items.AddRange(obj.HOBBies.Select(p => new ListItem { Text = p.HNAME, Value = p.HID.ToString() }).ToArray());
lb.Items.AddRange(obj.INTERESTs.Select(p => new ListItem { Text = p.INAME, Value = p.IID.ToString() }).ToArray());
ddl.Items.Insert(0, "Select");
ddl.Items.AddRange(obj.COUNTRies.Select(p => new ListItem { Text = p.CNAME, Value = p.CID.ToString() }).ToArray());
}
}
public void CLR()
{
txteid.Text = "";
txtname.Text = "";
txtadd.Text = "";
txtpwd.Text = "";
txtcpwd.Text = "";
rb.Items.Clear();
cbl.Items.Clear();
txtsal.Text = "";
txtemail.Text = "";
lb.Items.Clear();
ddl.Items.Clear();
ddl1.Items.Clear();
txtpwd.TextMode = TextBoxMode.Password;
txtcpwd.TextMode = TextBoxMode.Password;
rb.Items.Add("Male");
rb.Items.Add("Female");
ViewState["id"] = 0;
}
protected void btn_Click(object sender, EventArgs e)
{
using (Database1Entities obj = new Database1Entities())
{
EMP1 emp = new EMP1();
if (Convert.ToInt32(ViewState["id"]) == 0)
{
emp.EID = Convert.ToInt32(txteid.Text);
emp.NAME = txtname.Text;
emp.ADDRESS = txtadd.Text;
emp.PASSWORD = txtcpwd.Text;
emp.GENDER = rb.SelectedValue;
emp.SALARY = Convert.ToDecimal(txtsal.Text);
emp.EMAIL = txtemail.Text;
emp.CID = Convert.ToInt32(ddl.SelectedValue);
emp.SID = Convert.ToInt32(ddl1.SelectedValue);
obj.Entry(emp).State = EntityState.Added;
obj.SaveChanges();
List<HOBBYMAP> lsth = new List<HOBBYMAP>();
foreach (ListItem li in cbl.Items)
{
if (li.Selected == true)
lsth.Add(new HOBBYMAP { EID = emp.EID, HID = Convert.ToInt32(li.Value) });
}
obj.HOBBYMAPs.AddRange(lsth);
obj.SaveChanges();
List<INTERESTMAP> lstm = new List<INTERESTMAP>();
foreach (ListItem li in lb.Items)
{
if (li.Selected)
lstm.Add(new INTERESTMAP { EID = emp.EID, IID = Convert.ToInt32(li.Value) });
}
obj.INTERESTMAPs.AddRange(lstm);
obj.SaveChanges();
lbmess.Text = "Data Saved.";
}else {
int id = Convert.ToInt32(ViewState["id"]);
emp = obj.EMP1.Find(id);
emp.NAME = txtname.Text;
emp.ADDRESS = txtadd.Text;
emp.PASSWORD = txtcpwd.Text;
emp.GENDER = rb.SelectedValue;
emp.SALARY = Convert.ToDecimal(txtsal.Text);
emp.EMAIL = txtemail.Text;
emp.CID = Convert.ToInt32(ddl.SelectedValue);
emp.SID = Convert.ToInt32(ddl1.SelectedValue);
obj.Entry(emp).State = EntityState.Modified;
obj.SaveChanges();
obj.HOBBYMAPs.RemoveRange(obj.HOBBYMAPs.Where(x => x.EID == id));
obj.SaveChanges();
List<HOBBYMAP> lsth = new List<HOBBYMAP>();
foreach (ListItem li in cbl.Items)
{
if (li.Selected == true)
lsth.Add(new HOBBYMAP { EID = emp.EID, HID = Convert.ToInt32(li.Value) });
}
obj.HOBBYMAPs.AddRange(lsth);
obj.SaveChanges();
obj.INTERESTMAPs.RemoveRange(obj.INTERESTMAPs.Where(p => p.EID == id));
obj.SaveChanges();
List<INTERESTMAP> lstm = new List<INTERESTMAP>();
foreach (ListItem li in lb.Items)
{
if (li.Selected)
lstm.Add(new INTERESTMAP { EID = emp.EID, IID = Convert.ToInt32(li.Value) });
}
obj.INTERESTMAPs.AddRange(lstm);
obj.SaveChanges();
lbmess.Text = "Data Updated.";
}
}
CLR();
fill();
fillgv();
}
protected void ddl_SelectedIndexChanged(object sender, EventArgs e)
{
using (Database1Entities obj = new Database1Entities())
{
int cid =Convert.ToInt32(ddl.SelectedValue);
ddl1.Items.Clear();
ddl1.Items.Insert(0, "Select");
ddl1.Items.AddRange(obj.STATEs.Where(p => p.CID == cid).Select(r => new ListItem { Text = r.SNAME, Value = r.SID.ToString() }).ToArray());
}
}
public void fillgv()
{
using (Database1Entities obj = new Database1Entities())
{
gv.DataSource = obj.EMP1.ToList();
gv.DataBind();
}
}
public void fillfields(int id)
{
using (Database1Entities obj = new Database1Entities())
{
EMP1 emp = obj.EMP1.Find(id);
txteid.Text =Convert.ToString(emp.EID);
txtname.Text = emp.NAME;
txtadd.Text = emp.ADDRESS;
txtpwd.TextMode = TextBoxMode.SingleLine;
txtpwd.Text = emp.PASSWORD;
txtcpwd.TextMode = TextBoxMode.SingleLine;
txtcpwd.Text = emp.PASSWORD;
rb.SelectedValue = emp.GENDER;
fill();
cbl.Items.Clear();
List<int?> lsth = obj.HOBBYMAPs.Where(p => p.EID == id).Select(k=>k.HID).ToList();
List<ListItem> lst1 = obj.HOBBies.Where(p => lsth.Contains(p.HID)).Select(x => new ListItem { Text = x.HNAME, Value = x.HID.ToString(), Selected = true }).ToList();
List<ListItem> lst2 = obj.HOBBies.Where(p => lsth.Contains(p.HID) == false).Select(x => new ListItem { Value = x.HID.ToString(), Text = x.HNAME, Selected = false }).ToList();
cbl.Items.AddRange(lst1.Union(lst2).ToArray());
txtsal.Text =Convert.ToString( emp.SALARY);
txtemail.Text = emp.EMAIL;
List<int?> lsti = obj.INTERESTMAPs.Where(p => p.EID == id).Select(k => k.IID).ToList();
List<ListItem> lst3 = obj.INTERESTs.Where(p => lsti.Contains(p.IID)).Select(k => new ListItem { Value=k.IID.ToString(), Text=k.INAME, Selected=true }).ToList();
List<ListItem> lst4 = obj.INTERESTs.Where(p => lsti.Contains(p.IID)==false).Select(k => new ListItem { Value = k.IID.ToString(), Text = k.INAME, Selected = false }).ToList();
lb.Items.Clear();
lb.Items.AddRange(lst3.Union(lst4).ToArray());
ddl.SelectedValue =Convert.ToString(emp.CID);
ddl1.Items.Clear();
ddl1.Items.AddRange(obj.STATEs.Where(p=>p.CID==emp.CID).Select(x=>new ListItem{ Value=x.SID.ToString(), Text=x.SNAME }).ToArray());
ddl1.SelectedValue =Convert.ToString(emp.SID);
ViewState["id"] = id;
}
}
protected void gv_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
using (Database1Entities obj = new Database1Entities())
{
obj.Entry(obj.EMP1.Find(Convert.ToInt32(gv.Rows[e.RowIndex].Cells[0].Text))).State = EntityState.Deleted;
obj.SaveChanges();
fillgv();
}
}
protected void gv_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
gv.PageIndex = e.NewPageIndex;
fillgv();
}
protected void gv_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
{
int id = Convert.ToInt32(gv.Rows[e.NewSelectedIndex].Cells[0].Text);
fillfields(id);
fillgv();
}
protected void btn1_Click(object sender, EventArgs e)
{
CLR();
fill();
}
}
}
--------------------------------------------------------------------------------------------------------------------------
part-2
view page:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Index1.aspx.cs" Inherits="WebApplication9.Index1" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<table align="center">
<tr>
<td>EID</td>
<td><asp:TextBox ID="txteid" runat="server" Width="200"></asp:TextBox></td>
<td><asp:RequiredFieldValidator ID="rv" runat="server" ControlToValidate="txteid" ErrorMessage="EID should not be blank."></asp:RequiredFieldValidator></td>
</tr>
<tr>
<td>NAME</td>
<td><asp:TextBox ID="txtname" runat="server" Width="200"></asp:TextBox></td>
<td><asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="txtname" ErrorMessage="Name should not be blank."></asp:RequiredFieldValidator></td>
</tr>
<tr>
<td>ADDRESS</td>
<td><asp:TextBox ID="txtadd" TextMode="MultiLine" runat="server" Width="200"></asp:TextBox></td>
<td><asp:RequiredFieldValidator ID="RequiredFieldValidator5" runat="server" ControlToValidate="txtadd" ErrorMessage="Address should not be blank."></asp:RequiredFieldValidator></td>
</tr>
<tr>
<td>PASSWORD</td>
<td><asp:TextBox ID="txtpwd" TextMode="Password" MaxLength="8" runat="server" Width="200"></asp:TextBox></td>
<td><asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ControlToValidate="txtpwd" ErrorMessage="Password should not be blank."></asp:RequiredFieldValidator></td>
</tr>
<tr>
<td style="width:180px;">CONFIRM PASSWORD</td>
<td><asp:TextBox ID="txtcpwd" TextMode="Password" MaxLength="8" runat="server" Width="200"></asp:TextBox></td>
<td><asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" ControlToValidate="txtcpwd" ErrorMessage="Confirm password should not be blank."></asp:RequiredFieldValidator>
<asp:CompareValidator runat="server" ID="cv" ErrorMessage="Password and Confirm password must be same." ControlToValidate="txtcpwd" ControlToCompare="txtpwd" ></asp:CompareValidator>
</td>
</tr>
<tr>
<td>GENDER</td>
<td><asp:RadioButtonList ID="rb" runat="server" Width="200"></asp:RadioButtonList></td>
<td><asp:RequiredFieldValidator ID="RequiredFieldValidator4" runat="server" ControlToValidate="rb" ErrorMessage="Please select a gender."></asp:RequiredFieldValidator></td>
</tr>
<tr>
<td>HOBBY</td>
<td><asp:CheckBoxList Width="200" runat="server" ID="cbl"></asp:CheckBoxList></td>
<td><asp:CustomValidator runat="server" ID="cbl3" ClientValidationFunction="ValidateCheckBoxList" ErrorMessage="Please select a bhobby"></asp:CustomValidator></td>
</tr>
<tr>
<td>INTEREST</td>
<td><asp:ListBox ID="lb" runat="server" Width="200" SelectionMode="Multiple"></asp:ListBox></td>
<td><asp:RequiredFieldValidator ID="RequiredFieldValidator6" runat="server" ControlToValidate="lb" ErrorMessage="Please select a interest."></asp:RequiredFieldValidator></td>
</tr>
<tr>
<td>DOB</td>
<td><asp:TextBox ID="txtdob" runat="server" Width="200" ReadOnly="true"></asp:TextBox></td>
<td><asp:Calendar runat="server" ID="calen" OnSelectionChanged="calen_SelectionChanged"></asp:Calendar><asp:RequiredFieldValidator ID="RequiredFieldValidator7" runat="server" ControlToValidate="txtdob" ErrorMessage="DOB should not be blank."></asp:RequiredFieldValidator></td>
</tr>
<tr>
<td>EMAIL</td>
<td><asp:TextBox ID="txtemail" runat="server" Width="200" ></asp:TextBox></td>
<td><asp:RegularExpressionValidator ControlToValidate="txtemail" ErrorMessage="Invalid Email ID." ID="re" runat="server" ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"></asp:RegularExpressionValidator><asp:RequiredFieldValidator ID="RequiredFieldValidator8" runat="server" ControlToValidate="txtemail" ErrorMessage="Email should not be blank."></asp:RequiredFieldValidator></td>
</tr>
<tr>
<td>SALARY</td>
<td><asp:TextBox ID="txtsal" runat="server" Width="200"></asp:TextBox></td>
<td><asp:RangeValidator ID="rvs" ControlToValidate="txtsal" runat="server" ErrorMessage="Salary range between 5000 and 500000." Type="Integer" MaximumValue="500000" MinimumValue="5000"></asp:RangeValidator><asp:RequiredFieldValidator ID="RequiredFieldValidator9" runat="server" ControlToValidate="txtsal" ErrorMessage="Salary should not be blank."></asp:RequiredFieldValidator></td>
</tr>
<tr>
<td>COUNTRY</td>
<td><asp:DropDownList ID="ddl" runat="server" Width="200" AutoPostBack="True" OnSelectedIndexChanged="ddl_SelectedIndexChanged"></asp:DropDownList></td>
<td><asp:RequiredFieldValidator ID="RequiredFieldValidator10" runat="server" ControlToValidate="ddl" ErrorMessage="Please select a country."></asp:RequiredFieldValidator></td>
</tr>
<tr>
<td>STATE</td>
<td><asp:DropDownList ID="ddls" runat="server" Width="200"></asp:DropDownList></td>
<td><asp:RequiredFieldValidator ID="RequiredFieldValidator11" runat="server" ControlToValidate="ddls" ErrorMessage="Please select a state."></asp:RequiredFieldValidator></td>
</tr>
<tr>
<td></td>
<td>
<asp:Button ID="btnsave" runat="server" Text="Save" Width="80" OnClick="btnsave_Click" />
<asp:Button ID="btnreset" runat="server" Text="Reset" Width="80" OnClick="btnreset_Click" />
</td>
<td></td>
</tr>
<tr>
<td></td>
<td>
<asp:GridView runat="server" ID="gv" AutoGenerateColumns="false" CellPadding="4" ForeColor="#333333" GridLines="None" OnRowDeleting="gv_RowDeleting" OnSelectedIndexChanging="gv_SelectedIndexChanging" AllowPaging="True" OnPageIndexChanging="gv_PageIndexChanging" PageSize="2">
<AlternatingRowStyle BackColor="White" />
<EditRowStyle BackColor="#2461BF" />
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#EFF3FB" />
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#F5F7FB" />
<SortedAscendingHeaderStyle BackColor="#6D95E1" />
<SortedDescendingCellStyle BackColor="#E9EBEF" />
<SortedDescendingHeaderStyle BackColor="#4870BE" />
<Columns>
<asp:TemplateField HeaderText="EID">
<ItemTemplate>
<asp:Label ID="lb" runat="server" Text='<%#Eval("EID") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="NAME">
<ItemTemplate>
<asp:Label runat="server" ID="lbn" Text='<%#Eval("NAME") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:CommandField ShowSelectButton ShowDeleteButton HeaderText="ACTION" />
</Columns>
</asp:GridView>
</td>
<td></td>
</tr>
</table>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="ddl" EventName="SelectedIndexChanged" />
</Triggers>
</asp:UpdatePanel>
</div>
</form>
<script type="text/javascript">
function ValidateCheckBoxList(sender, args) {
var checkBoxList = document.getElementById("<%=cbl.ClientID %>");
var checkboxes = checkBoxList.getElementsByTagName("input");
var isValid = false;
for (var i = 0; i < checkboxes.length; i++) {
if (checkboxes[i].checked) {
isValid = true;
break;
}
}
args.IsValid = isValid;
}
</script>
</body>
</html>
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace WebApplication9
{
public partial class Admin : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
CLR();
fill();
fillgv();
}else
{
txtpwd.Attributes["value"] = txtpwd.Text;
txtcpwd.Attributes["value"] = txtcpwd.Text;
}
}
public void fill()
{
using (Database1Entities obj = new WebApplication9.Database1Entities())
{
cbl.Items.AddRange(obj.HOBBies.Select(p => new ListItem { Text = p.HNAME, Value = p.HID.ToString() }).ToArray());
lb.Items.AddRange(obj.INTERESTs.Select(p => new ListItem { Text = p.INAME, Value = p.IID.ToString() }).ToArray());
ddl.Items.Insert(0, "Select");
ddl.Items.AddRange(obj.COUNTRies.Select(p => new ListItem { Text = p.CNAME, Value = p.CID.ToString() }).ToArray());
}
}
public void CLR()
{
txteid.Text = "";
txtname.Text = "";
txtadd.Text = "";
txtpwd.Text = "";
txtcpwd.Text = "";
rb.Items.Clear();
cbl.Items.Clear();
txtsal.Text = "";
txtemail.Text = "";
lb.Items.Clear();
ddl.Items.Clear();
ddl1.Items.Clear();
txtpwd.TextMode = TextBoxMode.Password;
txtcpwd.TextMode = TextBoxMode.Password;
rb.Items.Add("Male");
rb.Items.Add("Female");
ViewState["id"] = 0;
}
protected void btn_Click(object sender, EventArgs e)
{
using (Database1Entities obj = new Database1Entities())
{
EMP1 emp = new EMP1();
if (Convert.ToInt32(ViewState["id"]) == 0)
{
emp.EID = Convert.ToInt32(txteid.Text);
emp.NAME = txtname.Text;
emp.ADDRESS = txtadd.Text;
emp.PASSWORD = txtcpwd.Text;
emp.GENDER = rb.SelectedValue;
emp.SALARY = Convert.ToDecimal(txtsal.Text);
emp.EMAIL = txtemail.Text;
emp.CID = Convert.ToInt32(ddl.SelectedValue);
emp.SID = Convert.ToInt32(ddl1.SelectedValue);
obj.Entry(emp).State = EntityState.Added;
obj.SaveChanges();
List<HOBBYMAP> lsth = new List<HOBBYMAP>();
foreach (ListItem li in cbl.Items)
{
if (li.Selected == true)
lsth.Add(new HOBBYMAP { EID = emp.EID, HID = Convert.ToInt32(li.Value) });
}
obj.HOBBYMAPs.AddRange(lsth);
obj.SaveChanges();
List<INTERESTMAP> lstm = new List<INTERESTMAP>();
foreach (ListItem li in lb.Items)
{
if (li.Selected)
lstm.Add(new INTERESTMAP { EID = emp.EID, IID = Convert.ToInt32(li.Value) });
}
obj.INTERESTMAPs.AddRange(lstm);
obj.SaveChanges();
lbmess.Text = "Data Saved.";
}else {
int id = Convert.ToInt32(ViewState["id"]);
emp = obj.EMP1.Find(id);
emp.NAME = txtname.Text;
emp.ADDRESS = txtadd.Text;
emp.PASSWORD = txtcpwd.Text;
emp.GENDER = rb.SelectedValue;
emp.SALARY = Convert.ToDecimal(txtsal.Text);
emp.EMAIL = txtemail.Text;
emp.CID = Convert.ToInt32(ddl.SelectedValue);
emp.SID = Convert.ToInt32(ddl1.SelectedValue);
obj.Entry(emp).State = EntityState.Modified;
obj.SaveChanges();
obj.HOBBYMAPs.RemoveRange(obj.HOBBYMAPs.Where(x => x.EID == id));
obj.SaveChanges();
List<HOBBYMAP> lsth = new List<HOBBYMAP>();
foreach (ListItem li in cbl.Items)
{
if (li.Selected == true)
lsth.Add(new HOBBYMAP { EID = emp.EID, HID = Convert.ToInt32(li.Value) });
}
obj.HOBBYMAPs.AddRange(lsth);
obj.SaveChanges();
obj.INTERESTMAPs.RemoveRange(obj.INTERESTMAPs.Where(p => p.EID == id));
obj.SaveChanges();
List<INTERESTMAP> lstm = new List<INTERESTMAP>();
foreach (ListItem li in lb.Items)
{
if (li.Selected)
lstm.Add(new INTERESTMAP { EID = emp.EID, IID = Convert.ToInt32(li.Value) });
}
obj.INTERESTMAPs.AddRange(lstm);
obj.SaveChanges();
lbmess.Text = "Data Updated.";
}
}
CLR();
fill();
fillgv();
}
protected void ddl_SelectedIndexChanged(object sender, EventArgs e)
{
using (Database1Entities obj = new Database1Entities())
{
int cid =Convert.ToInt32(ddl.SelectedValue);
ddl1.Items.Clear();
ddl1.Items.Insert(0, "Select");
ddl1.Items.AddRange(obj.STATEs.Where(p => p.CID == cid).Select(r => new ListItem { Text = r.SNAME, Value = r.SID.ToString() }).ToArray());
}
}
public void fillgv()
{
using (Database1Entities obj = new Database1Entities())
{
gv.DataSource = obj.EMP1.ToList();
gv.DataBind();
}
}
public void fillfields(int id)
{
using (Database1Entities obj = new Database1Entities())
{
EMP1 emp = obj.EMP1.Find(id);
txteid.Text =Convert.ToString(emp.EID);
txtname.Text = emp.NAME;
txtadd.Text = emp.ADDRESS;
txtpwd.TextMode = TextBoxMode.SingleLine;
txtpwd.Text = emp.PASSWORD;
txtcpwd.TextMode = TextBoxMode.SingleLine;
txtcpwd.Text = emp.PASSWORD;
rb.SelectedValue = emp.GENDER;
fill();
cbl.Items.Clear();
List<int?> lsth = obj.HOBBYMAPs.Where(p => p.EID == id).Select(k=>k.HID).ToList();
List<ListItem> lst1 = obj.HOBBies.Where(p => lsth.Contains(p.HID)).Select(x => new ListItem { Text = x.HNAME, Value = x.HID.ToString(), Selected = true }).ToList();
List<ListItem> lst2 = obj.HOBBies.Where(p => lsth.Contains(p.HID) == false).Select(x => new ListItem { Value = x.HID.ToString(), Text = x.HNAME, Selected = false }).ToList();
cbl.Items.AddRange(lst1.Union(lst2).ToArray());
txtsal.Text =Convert.ToString( emp.SALARY);
txtemail.Text = emp.EMAIL;
List<int?> lsti = obj.INTERESTMAPs.Where(p => p.EID == id).Select(k => k.IID).ToList();
List<ListItem> lst3 = obj.INTERESTs.Where(p => lsti.Contains(p.IID)).Select(k => new ListItem { Value=k.IID.ToString(), Text=k.INAME, Selected=true }).ToList();
List<ListItem> lst4 = obj.INTERESTs.Where(p => lsti.Contains(p.IID)==false).Select(k => new ListItem { Value = k.IID.ToString(), Text = k.INAME, Selected = false }).ToList();
lb.Items.Clear();
lb.Items.AddRange(lst3.Union(lst4).ToArray());
ddl.SelectedValue =Convert.ToString(emp.CID);
ddl1.Items.Clear();
ddl1.Items.AddRange(obj.STATEs.Where(p=>p.CID==emp.CID).Select(x=>new ListItem{ Value=x.SID.ToString(), Text=x.SNAME }).ToArray());
ddl1.SelectedValue =Convert.ToString(emp.SID);
ViewState["id"] = id;
}
}
protected void gv_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
using (Database1Entities obj = new Database1Entities())
{
obj.Entry(obj.EMP1.Find(Convert.ToInt32(gv.Rows[e.RowIndex].Cells[0].Text))).State = EntityState.Deleted;
obj.SaveChanges();
fillgv();
}
}
protected void gv_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
gv.PageIndex = e.NewPageIndex;
fillgv();
}
protected void gv_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
{
int id = Convert.ToInt32(gv.Rows[e.NewSelectedIndex].Cells[0].Text);
fillfields(id);
fillgv();
}
protected void btn1_Click(object sender, EventArgs e)
{
CLR();
fill();
}
}
}
--------------------------------------------------------------------------------------------------------------------------
part-2
view page:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Index1.aspx.cs" Inherits="WebApplication9.Index1" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<table align="center">
<tr>
<td>EID</td>
<td><asp:TextBox ID="txteid" runat="server" Width="200"></asp:TextBox></td>
<td><asp:RequiredFieldValidator ID="rv" runat="server" ControlToValidate="txteid" ErrorMessage="EID should not be blank."></asp:RequiredFieldValidator></td>
</tr>
<tr>
<td>NAME</td>
<td><asp:TextBox ID="txtname" runat="server" Width="200"></asp:TextBox></td>
<td><asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="txtname" ErrorMessage="Name should not be blank."></asp:RequiredFieldValidator></td>
</tr>
<tr>
<td>ADDRESS</td>
<td><asp:TextBox ID="txtadd" TextMode="MultiLine" runat="server" Width="200"></asp:TextBox></td>
<td><asp:RequiredFieldValidator ID="RequiredFieldValidator5" runat="server" ControlToValidate="txtadd" ErrorMessage="Address should not be blank."></asp:RequiredFieldValidator></td>
</tr>
<tr>
<td>PASSWORD</td>
<td><asp:TextBox ID="txtpwd" TextMode="Password" MaxLength="8" runat="server" Width="200"></asp:TextBox></td>
<td><asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ControlToValidate="txtpwd" ErrorMessage="Password should not be blank."></asp:RequiredFieldValidator></td>
</tr>
<tr>
<td style="width:180px;">CONFIRM PASSWORD</td>
<td><asp:TextBox ID="txtcpwd" TextMode="Password" MaxLength="8" runat="server" Width="200"></asp:TextBox></td>
<td><asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" ControlToValidate="txtcpwd" ErrorMessage="Confirm password should not be blank."></asp:RequiredFieldValidator>
<asp:CompareValidator runat="server" ID="cv" ErrorMessage="Password and Confirm password must be same." ControlToValidate="txtcpwd" ControlToCompare="txtpwd" ></asp:CompareValidator>
</td>
</tr>
<tr>
<td>GENDER</td>
<td><asp:RadioButtonList ID="rb" runat="server" Width="200"></asp:RadioButtonList></td>
<td><asp:RequiredFieldValidator ID="RequiredFieldValidator4" runat="server" ControlToValidate="rb" ErrorMessage="Please select a gender."></asp:RequiredFieldValidator></td>
</tr>
<tr>
<td>HOBBY</td>
<td><asp:CheckBoxList Width="200" runat="server" ID="cbl"></asp:CheckBoxList></td>
<td><asp:CustomValidator runat="server" ID="cbl3" ClientValidationFunction="ValidateCheckBoxList" ErrorMessage="Please select a bhobby"></asp:CustomValidator></td>
</tr>
<tr>
<td>INTEREST</td>
<td><asp:ListBox ID="lb" runat="server" Width="200" SelectionMode="Multiple"></asp:ListBox></td>
<td><asp:RequiredFieldValidator ID="RequiredFieldValidator6" runat="server" ControlToValidate="lb" ErrorMessage="Please select a interest."></asp:RequiredFieldValidator></td>
</tr>
<tr>
<td>DOB</td>
<td><asp:TextBox ID="txtdob" runat="server" Width="200" ReadOnly="true"></asp:TextBox></td>
<td><asp:Calendar runat="server" ID="calen" OnSelectionChanged="calen_SelectionChanged"></asp:Calendar><asp:RequiredFieldValidator ID="RequiredFieldValidator7" runat="server" ControlToValidate="txtdob" ErrorMessage="DOB should not be blank."></asp:RequiredFieldValidator></td>
</tr>
<tr>
<td>EMAIL</td>
<td><asp:TextBox ID="txtemail" runat="server" Width="200" ></asp:TextBox></td>
<td><asp:RegularExpressionValidator ControlToValidate="txtemail" ErrorMessage="Invalid Email ID." ID="re" runat="server" ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"></asp:RegularExpressionValidator><asp:RequiredFieldValidator ID="RequiredFieldValidator8" runat="server" ControlToValidate="txtemail" ErrorMessage="Email should not be blank."></asp:RequiredFieldValidator></td>
</tr>
<tr>
<td>SALARY</td>
<td><asp:TextBox ID="txtsal" runat="server" Width="200"></asp:TextBox></td>
<td><asp:RangeValidator ID="rvs" ControlToValidate="txtsal" runat="server" ErrorMessage="Salary range between 5000 and 500000." Type="Integer" MaximumValue="500000" MinimumValue="5000"></asp:RangeValidator><asp:RequiredFieldValidator ID="RequiredFieldValidator9" runat="server" ControlToValidate="txtsal" ErrorMessage="Salary should not be blank."></asp:RequiredFieldValidator></td>
</tr>
<tr>
<td>COUNTRY</td>
<td><asp:DropDownList ID="ddl" runat="server" Width="200" AutoPostBack="True" OnSelectedIndexChanged="ddl_SelectedIndexChanged"></asp:DropDownList></td>
<td><asp:RequiredFieldValidator ID="RequiredFieldValidator10" runat="server" ControlToValidate="ddl" ErrorMessage="Please select a country."></asp:RequiredFieldValidator></td>
</tr>
<tr>
<td>STATE</td>
<td><asp:DropDownList ID="ddls" runat="server" Width="200"></asp:DropDownList></td>
<td><asp:RequiredFieldValidator ID="RequiredFieldValidator11" runat="server" ControlToValidate="ddls" ErrorMessage="Please select a state."></asp:RequiredFieldValidator></td>
</tr>
<tr>
<td></td>
<td>
<asp:Button ID="btnsave" runat="server" Text="Save" Width="80" OnClick="btnsave_Click" />
<asp:Button ID="btnreset" runat="server" Text="Reset" Width="80" OnClick="btnreset_Click" />
</td>
<td></td>
</tr>
<tr>
<td></td>
<td>
<asp:GridView runat="server" ID="gv" AutoGenerateColumns="false" CellPadding="4" ForeColor="#333333" GridLines="None" OnRowDeleting="gv_RowDeleting" OnSelectedIndexChanging="gv_SelectedIndexChanging" AllowPaging="True" OnPageIndexChanging="gv_PageIndexChanging" PageSize="2">
<AlternatingRowStyle BackColor="White" />
<EditRowStyle BackColor="#2461BF" />
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#EFF3FB" />
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#F5F7FB" />
<SortedAscendingHeaderStyle BackColor="#6D95E1" />
<SortedDescendingCellStyle BackColor="#E9EBEF" />
<SortedDescendingHeaderStyle BackColor="#4870BE" />
<Columns>
<asp:TemplateField HeaderText="EID">
<ItemTemplate>
<asp:Label ID="lb" runat="server" Text='<%#Eval("EID") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="NAME">
<ItemTemplate>
<asp:Label runat="server" ID="lbn" Text='<%#Eval("NAME") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:CommandField ShowSelectButton ShowDeleteButton HeaderText="ACTION" />
</Columns>
</asp:GridView>
</td>
<td></td>
</tr>
</table>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="ddl" EventName="SelectedIndexChanged" />
</Triggers>
</asp:UpdatePanel>
</div>
</form>
<script type="text/javascript">
function ValidateCheckBoxList(sender, args) {
var checkBoxList = document.getElementById("<%=cbl.ClientID %>");
var checkboxes = checkBoxList.getElementsByTagName("input");
var isValid = false;
for (var i = 0; i < checkboxes.length; i++) {
if (checkboxes[i].checked) {
isValid = true;
break;
}
}
args.IsValid = isValid;
}
</script>
</body>
</html>
C# code :
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace WebApplication9
{
public partial class Index1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
fillcontrol();
fill();
}
else
{
txtpwd.Attributes["value"] = txtpwd.Text;
txtcpwd.Attributes["value"] = txtcpwd.Text;
}
}
protected void calen_SelectionChanged(object sender, EventArgs e)
{
txtdob.Text = calen.SelectedDate.ToShortDateString();
}
public void fillcontrol()
{
rb.Items.Add("Male");
rb.Items.Add("Female");
using (Database1Entities obj = new Database1Entities())
{
cbl.Items.AddRange(obj.HOBBies.Select(x => new ListItem { Value = x.HID.ToString(), Text = x.HNAME }).ToArray());
lb.Items.AddRange(obj.INTERESTs.Select(x => new ListItem { Value = x.IID.ToString(), Text = x.INAME }).ToArray());
ddl.Items.AddRange(obj.COUNTRies.Select(x => new ListItem { Value = x.CID.ToString(), Text = x.CNAME }).ToArray());
ddl.Items.Insert(0, "Select");
}
}
protected void ddl_SelectedIndexChanged(object sender, EventArgs e)
{
using (Database1Entities obj = new Database1Entities())
{
ddls.Items.Clear();
int id= Convert.ToInt32(ddl.SelectedValue);
ddls.Items.AddRange(obj.STATEs.Where(x=>x.CID==id).Select(p=>new ListItem { Value=p.SID.ToString(), Text=p.SNAME }).ToArray());
ddls.Items.Insert(0, "Select");
}
}
public void CLR()
{
txteid.Text = string.Empty;
txtname.Text = string.Empty;
txtadd.Text = string.Empty;
txtcpwd.Text = string.Empty;
rb.Items.Clear();
txtdob.Text = string.Empty;
txtemail.Text = string.Empty;
txtsal.Text = string.Empty;
txtpwd.Attributes["value"] = string.Empty;
txtcpwd.Attributes["value"] = string.Empty;
cbl.Items.Clear();
lb.Items.Clear();
ddl.Items.Clear();
ddls.Items.Clear();
fillcontrol();
ViewState["id"] = 0;
}
public void fill()
{
using (Database1Entities obj = new Database1Entities())
{
gv.DataSource = obj.EMP2.ToList();
gv.DataBind();
}
}
protected void btnsave_Click(object sender, EventArgs e)
{
using (Database1Entities obj = new Database1Entities())
{
if (Convert.ToInt32(ViewState["id"]) == 0)
{
EMP2 emp = new EMP2();
int eid = Convert.ToInt32(txteid.Text);
emp.EID = eid;
emp.NAME = txtname.Text;
emp.ADDRESS = txtadd.Text;
emp.PASSWORD = txtcpwd.Text;
emp.GENDER = rb.SelectedValue;
emp.DOB = Convert.ToDateTime(txtdob.Text);
emp.EMAIL = txtemail.Text;
emp.SALARY = Convert.ToDecimal(txtsal.Text);
emp.CID = Convert.ToInt32(ddl.SelectedValue);
emp.SID = Convert.ToInt32(ddls.SelectedValue);
obj.Entry(emp).State = EntityState.Added;
obj.SaveChanges();
List<INTERESTMAP> lst = new List<INTERESTMAP>();
foreach (ListItem li in lb.Items)
if (li.Selected)
lst.Add(new INTERESTMAP { EID = eid, IID = Convert.ToInt32(li.Value) });
obj.INTERESTMAPs.AddRange(lst);
obj.SaveChanges();
List<HOBBYMAP> lst1 = new List<HOBBYMAP>();
foreach (ListItem li in cbl.Items)
if (li.Selected)
lst1.Add(new HOBBYMAP { EID = eid, HID = Convert.ToInt32(li.Value) });
obj.HOBBYMAPs.AddRange(lst1);
obj.SaveChanges();
}
else
{
int eid = Convert.ToInt32(txteid.Text);
EMP2 emp = obj.EMP2.Find(eid);
emp.NAME = txtname.Text;
emp.ADDRESS = txtadd.Text;
emp.PASSWORD = txtcpwd.Text;
emp.GENDER = rb.SelectedValue;
emp.DOB = Convert.ToDateTime(txtdob.Text);
emp.EMAIL = txtemail.Text;
emp.SALARY = Convert.ToDecimal(txtsal.Text);
emp.CID = Convert.ToInt32(ddl.SelectedValue);
emp.SID = Convert.ToInt32(ddls.SelectedValue);
obj.Entry(emp).State = EntityState.Modified;
obj.SaveChanges();
obj.INTERESTMAPs.RemoveRange(obj.INTERESTMAPs.Where(p => p.EID == eid));
obj.SaveChanges();
List<INTERESTMAP> lst = new List<INTERESTMAP>();
foreach (ListItem li in lb.Items)
if (li.Selected)
lst.Add(new INTERESTMAP { EID = eid, IID = Convert.ToInt32(li.Value) });
obj.INTERESTMAPs.AddRange(lst);
obj.SaveChanges();
obj.HOBBYMAPs.RemoveRange(obj.HOBBYMAPs.Where(p => p.EID == eid));
obj.SaveChanges();
List<HOBBYMAP> lst1 = new List<HOBBYMAP>();
foreach (ListItem li in cbl.Items)
if (li.Selected)
lst1.Add(new HOBBYMAP { EID = eid, HID = Convert.ToInt32(li.Value) });
obj.HOBBYMAPs.AddRange(lst1);
obj.SaveChanges();
}
fill();
CLR();
}
}
protected void btnreset_Click(object sender, EventArgs e)
{
CLR();
}
protected void gv_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
{
int id = Convert.ToInt32(((Label)gv.Rows[e.NewSelectedIndex].Cells[0].FindControl("lb")).Text);
using (Database1Entities obj = new Database1Entities())
{
EMP2 emp = obj.EMP2.Find(id);
txteid.Text = emp.EID.ToString();
txtname.Text = emp.NAME;
txtadd.Text = emp.ADDRESS;
txtpwd.Attributes["value"] =emp.PASSWORD;
txtcpwd.Attributes["value"] = emp.PASSWORD;
rb.SelectedValue = emp.GENDER;
txtdob.Text = Convert.ToDateTime(emp.DOB).ToShortDateString();
txtemail.Text = emp.EMAIL;
string[] s = emp.SALARY.ToString().Split('.');
txtsal.Text = s[0];
ddl.SelectedValue = emp.CID.ToString();
ddls.Items.AddRange(obj.STATEs.Where(m => m.CID == emp.CID).Select(n=>new ListItem { Value=n.SID.ToString(), Text=n.SNAME } ).ToArray());
ddls.SelectedValue = emp.SID.ToString();
List<ListItem> lst = obj.HOBBies.Where(m => obj.HOBBYMAPs.Where(p => p.EID == id).Select(q => q.HID).Contains(m.HID)).Select(r=>new ListItem { Value=r.HID.ToString(), Text=r.HNAME, Selected=true }).ToList();
List<ListItem> lst1 = obj.HOBBies.Select(p => new ListItem { Value = p.HID.ToString(), Text = p.HNAME, Selected = false }).ToList();
List<ListItem> lst2 = lst1.Except(lst).ToList();
cbl.Items.Clear();
cbl.Items.AddRange(lst.Union(lst2).ToArray());
List<ListItem> lst3 = new List<ListItem>();
int mark;
foreach (INTEREST inte in obj.INTERESTs)
{
mark = 0;
foreach(INTERESTMAP im in obj.INTERESTMAPs.Where(p=>p.EID==id))
{
if (im.IID == inte.IID)
{
mark = 1;
break;
}
}
if (mark == 1)
lst3.Add(new ListItem { Value = inte.IID.ToString(), Text = inte.INAME, Selected = true });
else
lst3.Add(new ListItem { Value = inte.IID.ToString(), Text = inte.INAME, Selected = false });
}
lb.Items.Clear();
lb.Items.AddRange(lst3.ToArray());
ViewState["id"] = 1;
}
}
protected void gv_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
int id = Convert.ToInt32(((Label)gv.Rows[e.RowIndex].Cells[0].FindControl("lb")).Text);
using (Database1Entities obj = new Database1Entities())
{
EMP2 emp = obj.EMP2.Find(id);
obj.Entry(emp).State = EntityState.Deleted;
obj.SaveChanges();
obj.INTERESTMAPs.RemoveRange(obj.INTERESTMAPs.Where(p => p.EID == id));
obj.SaveChanges();
obj.HOBBYMAPs.RemoveRange(obj.HOBBYMAPs.Where(p => p.EID == id));
obj.SaveChanges();
fill();
}
}
protected void gv_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
gv.PageIndex = e.NewPageIndex;
fill();
}
}
}
--------------------------------------------------------------------------------------------------------------------------
cascading dropdown in gridview :
c# code :
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace WebApplication9
{
public partial class Test : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
using (Database1Entities obj = new Database1Entities())
{
ddl.DataSource = obj.DEPTs.ToList();
ddl.DataTextField = "DNAME";
ddl.DataValueField = "DID";
ddl.DataBind();
ddl.Items.Insert(0, "Select");
ddl1.DataSource = obj.COUNTRies.ToList();
ddl1.DataTextField = "CNAME";
ddl1.DataValueField = "CID";
ddl1.DataBind();
ddl1.Items.Insert(0, "Select");
}
fill();
CLR();
}
}
protected void btn_Click(object sender, EventArgs e)
{
using (Database1Entities obj = new Database1Entities())
{
EMP3 emp = new EMP3();
emp.EID = Convert.ToInt32(tb.Text);
emp.NAME = tb1.Text;
emp.DID = Convert.ToInt32(ddl.SelectedValue);
emp.CID = Convert.ToInt32(ddl1.SelectedValue);
emp.SID = Convert.ToInt32(ddl2.SelectedValue);
obj.Entry(emp).State = EntityState.Added;
obj.SaveChanges();
CLR();
fill();
}
}
public void CLR()
{
tb.Text = String.Empty;
tb1.Text = String.Empty;
ddl.SelectedIndex = 0;
ddl1.SelectedIndex = 0;
ddl2.SelectedIndex = 0;
tb.Focus();
}
public void fill()
{
using (Database1Entities obj = new Database1Entities())
{
var m = (from x in obj.EMP3
join y in obj.DEPTs
on x.DID equals y.DID
join z in obj.COUNTRies
on x.CID equals z.CID
join n in obj.STATEs
on x.SID equals n.SID
select new{x.EID,x.NAME,x.DID,y.DNAME,x.CID,z.CNAME,x.SID,n.SNAME }).ToList();
gv.DataSource = m;
gv.DataBind();
}
}
protected void btn1_Click(object sender, EventArgs e)
{
CLR();
}
protected void ddl1_SelectedIndexChanged(object sender, EventArgs e)
{
using (Database1Entities obj = new Database1Entities())
{
ddl2.Items.Clear();
int cid = Convert.ToInt32(ddl1.SelectedValue);
ddl2.DataSource = obj.STATEs.Where(m => m.CID == cid).ToList();
ddl2.DataTextField = "SNAME";
ddl2.DataValueField = "SID";
ddl2.DataBind();
ddl2.Items.Insert(0, "Select");
}
}
//gride view dropdown event
protected void ddlc_SelectedIndexChanged(object sender, EventArgs e)
{
GridViewRow gvr = (GridViewRow)((DropDownList)sender).NamingContainer;
DropDownList dlc = ((DropDownList)gvr.FindControl("ddlc"));
DropDownList dls = ((DropDownList)gvr.FindControl("ddls"));
int cid = Convert.ToInt32(dlc.SelectedValue);
using (Database1Entities obj = new Database1Entities())
{
dls.Items.Clear();
dls.DataSource = obj.STATEs.Where(m=>m.CID==cid).ToList();
dls.DataTextField = "SNAME";
dls.DataValueField = "SID";
dls.DataBind();
dls.Items.Insert(0, "Select");
}
}
protected void gv_RowEditing(object sender, GridViewEditEventArgs e)
{
gv.EditIndex = e.NewEditIndex;
fill();
using (Database1Entities obj = new Database1Entities())
{
string did = gv.DataKeys[e.NewEditIndex]["DID"].ToString();
DropDownList dl = ((DropDownList)gv.Rows[e.NewEditIndex].FindControl("dddl"));
dl.DataSource = obj.DEPTs.ToList();
dl.DataTextField = "DNAME";
dl.DataValueField = "DID";
dl.DataBind();
dl.SelectedValue = did;
string cid = gv.DataKeys[e.NewEditIndex]["CID"].ToString();
DropDownList dl1 = ((DropDownList)gv.Rows[e.NewEditIndex].FindControl("ddlc"));
dl1.DataSource = obj.COUNTRies.ToList();
dl1.DataTextField = "CNAME";
dl1.DataValueField = "CID";
dl1.DataBind();
dl1.SelectedValue = cid;
string sid = gv.DataKeys[e.NewEditIndex]["SID"].ToString();
DropDownList dl2 = ((DropDownList)gv.Rows[e.NewEditIndex].FindControl("ddls"));
dl2.DataSource = obj.STATEs.ToList();
dl2.DataTextField = "SNAME";
dl2.DataValueField = "SID";
dl2.DataBind();
dl2.SelectedValue = sid;
}
}
protected void gv_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
int eid = Convert.ToInt32(gv.DataKeys[e.RowIndex]["EID"].ToString());
using (Database1Entities obj = new Database1Entities())
{
EMP3 emp = obj.EMP3.Find(eid);
emp.NAME = ((TextBox)gv.Rows[e.RowIndex].FindControl("tb")).Text;
emp.DID = Convert.ToInt32(((DropDownList)gv.Rows[e.RowIndex].FindControl("dddl")).SelectedValue);
emp.CID = Convert.ToInt32(((DropDownList)gv.Rows[e.RowIndex].FindControl("ddlc")).SelectedValue);
emp.SID = Convert.ToInt32(((DropDownList)gv.Rows[e.RowIndex].FindControl("ddls")).SelectedValue);
obj.SaveChanges();
gv.EditIndex = -1;
fill();
}
}
protected void gv_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
int eid = Convert.ToInt32(gv.DataKeys[e.RowIndex]["EID"].ToString());
using (Database1Entities obj = new Database1Entities())
{
obj.Entry(obj.EMP3.Find(eid)).State = EntityState.Deleted;
obj.SaveChanges();
fill();
}
}
protected void gv_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
gv.EditIndex = -1;
fill();
}
protected void gv_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
gv.PageIndex = e.NewPageIndex;
fill();
}
}
}
View or HTML code :
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Test.aspx.cs" Inherits="WebApplication9.Test" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<table align="center">
<tr>
<td>EID</td>
<td>
<asp:TextBox ID="tb" runat="server" Width="200"></asp:TextBox>
</td>
</tr>
<tr>
<td>NAME</td>
<td>
<asp:TextBox ID="tb1" runat="server" Width="200"></asp:TextBox>
</td>
</tr>
<tr>
<td>DEPARTMENT</td>
<td>
<asp:DropDownList ID="ddl" runat="server" Width="203"></asp:DropDownList>
</td>
</tr>
<tr>
<td>COUNTRY</td>
<td>
<asp:DropDownList ID="ddl1" runat="server" Width="203" AutoPostBack="True" OnSelectedIndexChanged="ddl1_SelectedIndexChanged"></asp:DropDownList>
</td>
</tr>
<tr>
<td>STATE</td>
<td>
<asp:DropDownList ID="ddl2" runat="server" Width="203"></asp:DropDownList>
</td>
</tr>
<tr>
<td></td>
<td>
<asp:Button ID="btn" runat="server" Text="Submit" Width="80" OnClick="btn_Click" />
<asp:Button ID="btn1" runat="server" Text="Reset" Width="80" OnClick="btn1_Click" />
</td>
</tr>
<tr>
<td></td>
<td>
<asp:GridView ID="gv" DataKeyNames="EID,CID,SID,DID" AutoGenerateColumns="False" runat="server" CellPadding="4" ForeColor="#333333" GridLines="None" OnRowCancelingEdit="gv_RowCancelingEdit" OnRowDeleting="gv_RowDeleting" OnRowEditing="gv_RowEditing" OnRowUpdating="gv_RowUpdating" AllowPaging="True" OnPageIndexChanging="gv_PageIndexChanging" PageSize="1">
<AlternatingRowStyle BackColor="White" />
<Columns>
<asp:TemplateField HeaderText="NAME">
<ItemTemplate>
<asp:Label ID="lb" runat="server" Text='<%#Eval("NAME") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="tb" runat="server" Text='<%#Eval("NAME") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="DEPARTMENT">
<ItemTemplate>
<asp:Label ID="lb1" runat="server" Text='<%#Eval("DNAME") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID="dddl" runat="server" Width="100"></asp:DropDownList>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="COUNTRY">
<ItemTemplate>
<asp:Label ID="lb2" runat="server" Text='<%#Eval("CNAME") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID="ddlc" runat="server" Width="100" AutoPostBack="True" OnSelectedIndexChanged="ddlc_SelectedIndexChanged"></asp:DropDownList>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="STATE">
<ItemTemplate>
<asp:Label ID="lb3" runat="server" Text='<%#Eval("SNAME") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID="ddls" runat="server" Width="100"></asp:DropDownList>
</EditItemTemplate>
</asp:TemplateField>
<asp:CommandField HeaderText="ACTION" ShowEditButton="true" ShowDeleteButton="true" />
</Columns>
<EditRowStyle BackColor="#2461BF" />
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#EFF3FB" />
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#F5F7FB" />
<SortedAscendingHeaderStyle BackColor="#6D95E1" />
<SortedDescendingCellStyle BackColor="#E9EBEF" />
<SortedDescendingHeaderStyle BackColor="#4870BE" />
</asp:GridView>
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
--------------------------------------------------------------------------------------------------------------------------Nested Gride :
C# Code :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace WebApplication9
{
public partial class Test2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
fill();
}
public void fill() {
using (Database1Entities obj = new Database1Entities())
{
gv.DataSource = obj.DEPTs.ToList();
gv.DataBind();
}
}
protected void gv_RowEditing(object sender, GridViewEditEventArgs e)
{
gv.EditIndex = e.NewEditIndex;
fill();
int did = Convert.ToInt32(gv.DataKeys[e.NewEditIndex]["DID"].ToString());
using (Database1Entities obj = new Database1Entities())
{
GridView gv2 = ((GridView)gv.Rows[e.NewEditIndex].FindControl("gv1"));
gv2.DataSource = obj.EMP3.Where(x=>x.DID==did).ToList();
gv2.DataBind();
}
}
protected void gv_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
gv.EditIndex = -1;
fill();
}
}
}
Html View :
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Test2.aspx.cs" Inherits="WebApplication9.Test2" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<table align="center">
<tr>
<td>
<asp:GridView ID="gv" runat="server" AutoGenerateColumns="False" DataKeyNames="DID" OnRowCancelingEdit="gv_RowCancelingEdit" OnRowEditing="gv_RowEditing" CellPadding="4" ForeColor="#333333" GridLines="None">
<AlternatingRowStyle BackColor="White" />
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="lb" runat="server" Font-Underline="false" Text="+" CommandName="Edit"></asp:LinkButton>
</ItemTemplate>
<EditItemTemplate>
<asp:LinkButton ID="lb1" runat="server" Font-Underline="false" Text="-" CommandName="Cancel"></asp:LinkButton>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="DID">
<ItemTemplate>
<asp:Label ID="lb2" runat="server" Text='<%#Eval("DID") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:GridView ID="gv1" runat="server"></asp:GridView>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="DNAME">
<ItemTemplate>
<asp:Label ID="lb3" runat="server" Text='<%#Eval("DNAME") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
</EditItemTemplate>
</asp:TemplateField>
</Columns>
<FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#FFCC66" ForeColor="#333333" HorizontalAlign="Center" />
<RowStyle BackColor="#FFFBD6" ForeColor="#333333" />
<SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="Navy" />
<SortedAscendingCellStyle BackColor="#FDF5AC" />
<SortedAscendingHeaderStyle BackColor="#4D0000" />
<SortedDescendingCellStyle BackColor="#FCF6C0" />
<SortedDescendingHeaderStyle BackColor="#820000" />
</asp:GridView>
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
-------------------------------------------------------------------------------------------------------------------------- Other Process Of Nested Gride View:
View Model :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace WebApplication9
{
public class DEPTVM
{
public int DID { get; set; }
public string DNAME { get; set; }
public List<EMP3> LEMP { get; set; }
}
}
C# code :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace WebApplication9
{
public partial class Test2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
fill();
}
public void fill() {
using (Database1Entities obj = new Database1Entities())
{
gv.DataSource = obj.DEPTs.Select(x => new DEPTVM { DID = x.DID, DNAME = x.DNAME, LEMP = obj.EMP3.Where(p => p.DID == x.DID).ToList() }).ToList();
gv.DataBind();
}
}
protected void gv_RowEditing(object sender, GridViewEditEventArgs e)
{
gv.EditIndex = e.NewEditIndex;
fill();
}
protected void gv_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
gv.EditIndex = -1;
fill();
}
}
}
Html View :
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Test2.aspx.cs" Inherits="WebApplication9.Test2" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<table align="center">
<tr>
<td>
<asp:GridView ID="gv" runat="server" AutoGenerateColumns="False" DataKeyNames="DID" OnRowCancelingEdit="gv_RowCancelingEdit" OnRowEditing="gv_RowEditing" CellPadding="4" ForeColor="#333333" GridLines="None">
<AlternatingRowStyle BackColor="White" />
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="lb" runat="server" Font-Underline="false" Text="+" CommandName="Edit"></asp:LinkButton>
</ItemTemplate>
<EditItemTemplate>
<asp:LinkButton ID="lb1" runat="server" Font-Underline="false" Text="-" CommandName="Cancel"></asp:LinkButton>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="DID">
<ItemTemplate>
<asp:Label ID="lb2" runat="server" Text='<%#Eval("DID") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:GridView ID="gv1" runat="server" DataSource='<%#Eval("LEMP") %>'></asp:GridView>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="DNAME">
<ItemTemplate>
<asp:Label ID="lb3" runat="server" Text='<%#Eval("DNAME") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
</EditItemTemplate>
</asp:TemplateField>
</Columns>
<FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#FFCC66" ForeColor="#333333" HorizontalAlign="Center" />
<RowStyle BackColor="#FFFBD6" ForeColor="#333333" />
<SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="Navy" />
<SortedAscendingCellStyle BackColor="#FDF5AC" />
<SortedAscendingHeaderStyle BackColor="#4D0000" />
<SortedDescendingCellStyle BackColor="#FCF6C0" />
<SortedDescendingHeaderStyle BackColor="#820000" />
</asp:GridView>
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
--------------------------------------------------------------------------------------------------------------------------
cascading dropdown in gridview :
c# code :
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace WebApplication9
{
public partial class Test : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
using (Database1Entities obj = new Database1Entities())
{
ddl.DataSource = obj.DEPTs.ToList();
ddl.DataTextField = "DNAME";
ddl.DataValueField = "DID";
ddl.DataBind();
ddl.Items.Insert(0, "Select");
ddl1.DataSource = obj.COUNTRies.ToList();
ddl1.DataTextField = "CNAME";
ddl1.DataValueField = "CID";
ddl1.DataBind();
ddl1.Items.Insert(0, "Select");
}
fill();
CLR();
}
}
protected void btn_Click(object sender, EventArgs e)
{
using (Database1Entities obj = new Database1Entities())
{
EMP3 emp = new EMP3();
emp.EID = Convert.ToInt32(tb.Text);
emp.NAME = tb1.Text;
emp.DID = Convert.ToInt32(ddl.SelectedValue);
emp.CID = Convert.ToInt32(ddl1.SelectedValue);
emp.SID = Convert.ToInt32(ddl2.SelectedValue);
obj.Entry(emp).State = EntityState.Added;
obj.SaveChanges();
CLR();
fill();
}
}
public void CLR()
{
tb.Text = String.Empty;
tb1.Text = String.Empty;
ddl.SelectedIndex = 0;
ddl1.SelectedIndex = 0;
ddl2.SelectedIndex = 0;
tb.Focus();
}
public void fill()
{
using (Database1Entities obj = new Database1Entities())
{
var m = (from x in obj.EMP3
join y in obj.DEPTs
on x.DID equals y.DID
join z in obj.COUNTRies
on x.CID equals z.CID
join n in obj.STATEs
on x.SID equals n.SID
select new{x.EID,x.NAME,x.DID,y.DNAME,x.CID,z.CNAME,x.SID,n.SNAME }).ToList();
gv.DataSource = m;
gv.DataBind();
}
}
protected void btn1_Click(object sender, EventArgs e)
{
CLR();
}
protected void ddl1_SelectedIndexChanged(object sender, EventArgs e)
{
using (Database1Entities obj = new Database1Entities())
{
ddl2.Items.Clear();
int cid = Convert.ToInt32(ddl1.SelectedValue);
ddl2.DataSource = obj.STATEs.Where(m => m.CID == cid).ToList();
ddl2.DataTextField = "SNAME";
ddl2.DataValueField = "SID";
ddl2.DataBind();
ddl2.Items.Insert(0, "Select");
}
}
//gride view dropdown event
protected void ddlc_SelectedIndexChanged(object sender, EventArgs e)
{
GridViewRow gvr = (GridViewRow)((DropDownList)sender).NamingContainer;
DropDownList dlc = ((DropDownList)gvr.FindControl("ddlc"));
DropDownList dls = ((DropDownList)gvr.FindControl("ddls"));
int cid = Convert.ToInt32(dlc.SelectedValue);
using (Database1Entities obj = new Database1Entities())
{
dls.Items.Clear();
dls.DataSource = obj.STATEs.Where(m=>m.CID==cid).ToList();
dls.DataTextField = "SNAME";
dls.DataValueField = "SID";
dls.DataBind();
dls.Items.Insert(0, "Select");
}
}
protected void gv_RowEditing(object sender, GridViewEditEventArgs e)
{
gv.EditIndex = e.NewEditIndex;
fill();
using (Database1Entities obj = new Database1Entities())
{
string did = gv.DataKeys[e.NewEditIndex]["DID"].ToString();
DropDownList dl = ((DropDownList)gv.Rows[e.NewEditIndex].FindControl("dddl"));
dl.DataSource = obj.DEPTs.ToList();
dl.DataTextField = "DNAME";
dl.DataValueField = "DID";
dl.DataBind();
dl.SelectedValue = did;
string cid = gv.DataKeys[e.NewEditIndex]["CID"].ToString();
DropDownList dl1 = ((DropDownList)gv.Rows[e.NewEditIndex].FindControl("ddlc"));
dl1.DataSource = obj.COUNTRies.ToList();
dl1.DataTextField = "CNAME";
dl1.DataValueField = "CID";
dl1.DataBind();
dl1.SelectedValue = cid;
string sid = gv.DataKeys[e.NewEditIndex]["SID"].ToString();
DropDownList dl2 = ((DropDownList)gv.Rows[e.NewEditIndex].FindControl("ddls"));
dl2.DataSource = obj.STATEs.ToList();
dl2.DataTextField = "SNAME";
dl2.DataValueField = "SID";
dl2.DataBind();
dl2.SelectedValue = sid;
}
}
protected void gv_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
int eid = Convert.ToInt32(gv.DataKeys[e.RowIndex]["EID"].ToString());
using (Database1Entities obj = new Database1Entities())
{
EMP3 emp = obj.EMP3.Find(eid);
emp.NAME = ((TextBox)gv.Rows[e.RowIndex].FindControl("tb")).Text;
emp.DID = Convert.ToInt32(((DropDownList)gv.Rows[e.RowIndex].FindControl("dddl")).SelectedValue);
emp.CID = Convert.ToInt32(((DropDownList)gv.Rows[e.RowIndex].FindControl("ddlc")).SelectedValue);
emp.SID = Convert.ToInt32(((DropDownList)gv.Rows[e.RowIndex].FindControl("ddls")).SelectedValue);
obj.SaveChanges();
gv.EditIndex = -1;
fill();
}
}
protected void gv_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
int eid = Convert.ToInt32(gv.DataKeys[e.RowIndex]["EID"].ToString());
using (Database1Entities obj = new Database1Entities())
{
obj.Entry(obj.EMP3.Find(eid)).State = EntityState.Deleted;
obj.SaveChanges();
fill();
}
}
protected void gv_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
gv.EditIndex = -1;
fill();
}
protected void gv_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
gv.PageIndex = e.NewPageIndex;
fill();
}
}
}
View or HTML code :
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Test.aspx.cs" Inherits="WebApplication9.Test" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<table align="center">
<tr>
<td>EID</td>
<td>
<asp:TextBox ID="tb" runat="server" Width="200"></asp:TextBox>
</td>
</tr>
<tr>
<td>NAME</td>
<td>
<asp:TextBox ID="tb1" runat="server" Width="200"></asp:TextBox>
</td>
</tr>
<tr>
<td>DEPARTMENT</td>
<td>
<asp:DropDownList ID="ddl" runat="server" Width="203"></asp:DropDownList>
</td>
</tr>
<tr>
<td>COUNTRY</td>
<td>
<asp:DropDownList ID="ddl1" runat="server" Width="203" AutoPostBack="True" OnSelectedIndexChanged="ddl1_SelectedIndexChanged"></asp:DropDownList>
</td>
</tr>
<tr>
<td>STATE</td>
<td>
<asp:DropDownList ID="ddl2" runat="server" Width="203"></asp:DropDownList>
</td>
</tr>
<tr>
<td></td>
<td>
<asp:Button ID="btn" runat="server" Text="Submit" Width="80" OnClick="btn_Click" />
<asp:Button ID="btn1" runat="server" Text="Reset" Width="80" OnClick="btn1_Click" />
</td>
</tr>
<tr>
<td></td>
<td>
<asp:GridView ID="gv" DataKeyNames="EID,CID,SID,DID" AutoGenerateColumns="False" runat="server" CellPadding="4" ForeColor="#333333" GridLines="None" OnRowCancelingEdit="gv_RowCancelingEdit" OnRowDeleting="gv_RowDeleting" OnRowEditing="gv_RowEditing" OnRowUpdating="gv_RowUpdating" AllowPaging="True" OnPageIndexChanging="gv_PageIndexChanging" PageSize="1">
<AlternatingRowStyle BackColor="White" />
<Columns>
<asp:TemplateField HeaderText="NAME">
<ItemTemplate>
<asp:Label ID="lb" runat="server" Text='<%#Eval("NAME") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="tb" runat="server" Text='<%#Eval("NAME") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="DEPARTMENT">
<ItemTemplate>
<asp:Label ID="lb1" runat="server" Text='<%#Eval("DNAME") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID="dddl" runat="server" Width="100"></asp:DropDownList>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="COUNTRY">
<ItemTemplate>
<asp:Label ID="lb2" runat="server" Text='<%#Eval("CNAME") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID="ddlc" runat="server" Width="100" AutoPostBack="True" OnSelectedIndexChanged="ddlc_SelectedIndexChanged"></asp:DropDownList>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="STATE">
<ItemTemplate>
<asp:Label ID="lb3" runat="server" Text='<%#Eval("SNAME") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID="ddls" runat="server" Width="100"></asp:DropDownList>
</EditItemTemplate>
</asp:TemplateField>
<asp:CommandField HeaderText="ACTION" ShowEditButton="true" ShowDeleteButton="true" />
</Columns>
<EditRowStyle BackColor="#2461BF" />
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#EFF3FB" />
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#F5F7FB" />
<SortedAscendingHeaderStyle BackColor="#6D95E1" />
<SortedDescendingCellStyle BackColor="#E9EBEF" />
<SortedDescendingHeaderStyle BackColor="#4870BE" />
</asp:GridView>
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
--------------------------------------------------------------------------------------------------------------------------Nested Gride :
C# Code :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace WebApplication9
{
public partial class Test2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
fill();
}
public void fill() {
using (Database1Entities obj = new Database1Entities())
{
gv.DataSource = obj.DEPTs.ToList();
gv.DataBind();
}
}
protected void gv_RowEditing(object sender, GridViewEditEventArgs e)
{
gv.EditIndex = e.NewEditIndex;
fill();
int did = Convert.ToInt32(gv.DataKeys[e.NewEditIndex]["DID"].ToString());
using (Database1Entities obj = new Database1Entities())
{
GridView gv2 = ((GridView)gv.Rows[e.NewEditIndex].FindControl("gv1"));
gv2.DataSource = obj.EMP3.Where(x=>x.DID==did).ToList();
gv2.DataBind();
}
}
protected void gv_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
gv.EditIndex = -1;
fill();
}
}
}
Html View :
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Test2.aspx.cs" Inherits="WebApplication9.Test2" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<table align="center">
<tr>
<td>
<asp:GridView ID="gv" runat="server" AutoGenerateColumns="False" DataKeyNames="DID" OnRowCancelingEdit="gv_RowCancelingEdit" OnRowEditing="gv_RowEditing" CellPadding="4" ForeColor="#333333" GridLines="None">
<AlternatingRowStyle BackColor="White" />
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="lb" runat="server" Font-Underline="false" Text="+" CommandName="Edit"></asp:LinkButton>
</ItemTemplate>
<EditItemTemplate>
<asp:LinkButton ID="lb1" runat="server" Font-Underline="false" Text="-" CommandName="Cancel"></asp:LinkButton>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="DID">
<ItemTemplate>
<asp:Label ID="lb2" runat="server" Text='<%#Eval("DID") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:GridView ID="gv1" runat="server"></asp:GridView>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="DNAME">
<ItemTemplate>
<asp:Label ID="lb3" runat="server" Text='<%#Eval("DNAME") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
</EditItemTemplate>
</asp:TemplateField>
</Columns>
<FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#FFCC66" ForeColor="#333333" HorizontalAlign="Center" />
<RowStyle BackColor="#FFFBD6" ForeColor="#333333" />
<SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="Navy" />
<SortedAscendingCellStyle BackColor="#FDF5AC" />
<SortedAscendingHeaderStyle BackColor="#4D0000" />
<SortedDescendingCellStyle BackColor="#FCF6C0" />
<SortedDescendingHeaderStyle BackColor="#820000" />
</asp:GridView>
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
-------------------------------------------------------------------------------------------------------------------------- Other Process Of Nested Gride View:
View Model :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace WebApplication9
{
public class DEPTVM
{
public int DID { get; set; }
public string DNAME { get; set; }
public List<EMP3> LEMP { get; set; }
}
}
C# code :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace WebApplication9
{
public partial class Test2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
fill();
}
public void fill() {
using (Database1Entities obj = new Database1Entities())
{
gv.DataSource = obj.DEPTs.Select(x => new DEPTVM { DID = x.DID, DNAME = x.DNAME, LEMP = obj.EMP3.Where(p => p.DID == x.DID).ToList() }).ToList();
gv.DataBind();
}
}
protected void gv_RowEditing(object sender, GridViewEditEventArgs e)
{
gv.EditIndex = e.NewEditIndex;
fill();
}
protected void gv_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
gv.EditIndex = -1;
fill();
}
}
}
Html View :
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Test2.aspx.cs" Inherits="WebApplication9.Test2" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<table align="center">
<tr>
<td>
<asp:GridView ID="gv" runat="server" AutoGenerateColumns="False" DataKeyNames="DID" OnRowCancelingEdit="gv_RowCancelingEdit" OnRowEditing="gv_RowEditing" CellPadding="4" ForeColor="#333333" GridLines="None">
<AlternatingRowStyle BackColor="White" />
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="lb" runat="server" Font-Underline="false" Text="+" CommandName="Edit"></asp:LinkButton>
</ItemTemplate>
<EditItemTemplate>
<asp:LinkButton ID="lb1" runat="server" Font-Underline="false" Text="-" CommandName="Cancel"></asp:LinkButton>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="DID">
<ItemTemplate>
<asp:Label ID="lb2" runat="server" Text='<%#Eval("DID") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:GridView ID="gv1" runat="server" DataSource='<%#Eval("LEMP") %>'></asp:GridView>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="DNAME">
<ItemTemplate>
<asp:Label ID="lb3" runat="server" Text='<%#Eval("DNAME") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
</EditItemTemplate>
</asp:TemplateField>
</Columns>
<FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#FFCC66" ForeColor="#333333" HorizontalAlign="Center" />
<RowStyle BackColor="#FFFBD6" ForeColor="#333333" />
<SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="Navy" />
<SortedAscendingCellStyle BackColor="#FDF5AC" />
<SortedAscendingHeaderStyle BackColor="#4D0000" />
<SortedDescendingCellStyle BackColor="#FCF6C0" />
<SortedDescendingHeaderStyle BackColor="#820000" />
</asp:GridView>
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
No comments:
Post a Comment