design the page :
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default6.aspx.cs" Inherits="Default6" %>
<!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">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="sm" runat="server"></asp:ScriptManager>
<div>
<asp:UpdatePanel ID="up" runat="server">
<ContentTemplate>
<table align="center">
<tr><td> </td><td><asp:Label ID="lb" runat="server"></asp:Label></td></tr>
<tr><td>EID</td><td><asp:TextBox ID="tb" runat="server"></asp:TextBox></td></tr>
<tr><td>NAME</td><td><asp:TextBox ID="tb1" runat="server"></asp:TextBox></td></tr>
<tr><td>SALARY</td><td><asp:TextBox ID="tb2" runat="server"></asp:TextBox></td></tr>
<tr><td> </td><td>
<asp:Button ID="btn" runat="server" Text="Save" Width="80" onclick="btn_Click"
/> <asp:Button ID="Button1" runat="server" Text="Reset" Width="80" /></td></tr>
<tr><td> </td><td><asp:DataGrid ID="dg" DataKeyField="EID" runat="server"
AutoGenerateColumns="false" AllowPaging="True" AllowSorting="True"
GridLines="Horizontal" onpageindexchanged="dg_PageIndexChanged"
PageSize="2" Width="100%" oncancelcommand="dg_CancelCommand"
ondeletecommand="dg_DeleteCommand" oneditcommand="dg_EditCommand"
onitemdatabound="dg_ItemDataBound" onupdatecommand="dg_UpdateCommand"
ShowFooter="True" >
<Columns>
<asp:TemplateColumn HeaderText="Sl No.">
<ItemTemplate><%#Container.ItemIndex+1 %></ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="EID">
<ItemTemplate><%#Eval("eid") %></ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="NAME">
<ItemTemplate><%#Eval("name") %></ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="tb3" runat="server" Text='<%#Eval("name") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="SALARY">
<ItemTemplate><asp:Label ID="lb9" runat="server" Text='<%#Eval("sal") %>'></asp:Label></ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="tb4" runat="server" Text='<%#Eval("SAL") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="UPDATE">
<ItemTemplate><asp:LinkButton ID="lb1" runat="server" CommandName="edit" Text="Edit"></asp:LinkButton></ItemTemplate>
<EditItemTemplate><asp:LinkButton ID="lb2" runat="server" CommandName="Cancel" Text="Cancel"></asp:LinkButton> <asp:LinkButton ID="lb3" runat="server" CommandName="update" Text="Update"></asp:LinkButton></EditItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="DELETE">
<ItemTemplate><asp:LinkButton ID="lb4" runat="server" CommandName="delete" Text="Delete"></asp:LinkButton></ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:DataGrid></td>
</table>
</ContentTemplate>
</asp:UpdatePanel>
</div>
</form>
</body>
</html>
The page looks like :
C# code :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DatabaseModel;
public partial class Default6 : System.Web.UI.Page
{
DatabaseEntities1 o = new DatabaseEntities1();
int i, j;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
view();
}
void view()
{
var m = from x in o.EMPs select x;
dg.DataSource = m;
dg.DataBind();
}
protected void dg_PageIndexChanged(object source, DataGridPageChangedEventArgs e)
{
dg.CurrentPageIndex = e.NewPageIndex;
view();
}
protected void dg_EditCommand(object source, DataGridCommandEventArgs e)
{
dg.EditItemIndex = e.Item.ItemIndex;
view();
}
protected void dg_CancelCommand(object source, DataGridCommandEventArgs e)
{
dg.EditItemIndex = -1;
view();
}
protected void dg_UpdateCommand(object source, DataGridCommandEventArgs e)
{
i = Convert.ToInt32(dg.DataKeys[e.Item.ItemIndex].ToString());
var m =( from x in o.EMPs where x.EID==i select x).First();
m.NAME = ((TextBox)dg.Items[e.Item.ItemIndex].FindControl("tb3")).Text;
m.SAL=Convert.ToInt32(((TextBox)dg.Items[e.Item.ItemIndex].FindControl("tb4")).Text);
o.SaveChanges();
lb.Text = "One record updated.";
dg.EditItemIndex = -1;
view();
}
protected void dg_DeleteCommand(object source, DataGridCommandEventArgs e)
{
i = Convert.ToInt32(dg.DataKeys[e.Item.ItemIndex].ToString());
var m = (from x in o.EMPs where x.EID == i select x).First();
o.EMPs.DeleteObject(m);
o.SaveChanges();
lb.Text = "One record delete.";
dg.CurrentPageIndex = 0;
view();
}
protected void dg_ItemDataBound(object sender, DataGridItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType==ListItemType.AlternatingItem)
{
j = j + Convert.ToInt32(((Label) e.Item.Cells[3].FindControl("lb9")).Text);
}
if (e.Item.ItemType == ListItemType.Footer)
{
e.Item.Cells[1].Text = "Total";
e.Item.Cells[3].Text = j.ToString();
}
}
protected void btn_Click(object sender, EventArgs e)
{
EMP p = new EMP();
p.EID = Convert.ToInt32(tb.Text);
p.NAME = tb1.Text;
p.SAL = Convert.ToInt32(tb2.Text);
o.EMPs.AddObject(p);
o.SaveChanges();
lb.Text = "One record saved.";
view();
}
}
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default6.aspx.cs" Inherits="Default6" %>
<!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">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="sm" runat="server"></asp:ScriptManager>
<div>
<asp:UpdatePanel ID="up" runat="server">
<ContentTemplate>
<table align="center">
<tr><td> </td><td><asp:Label ID="lb" runat="server"></asp:Label></td></tr>
<tr><td>EID</td><td><asp:TextBox ID="tb" runat="server"></asp:TextBox></td></tr>
<tr><td>NAME</td><td><asp:TextBox ID="tb1" runat="server"></asp:TextBox></td></tr>
<tr><td>SALARY</td><td><asp:TextBox ID="tb2" runat="server"></asp:TextBox></td></tr>
<tr><td> </td><td>
<asp:Button ID="btn" runat="server" Text="Save" Width="80" onclick="btn_Click"
/> <asp:Button ID="Button1" runat="server" Text="Reset" Width="80" /></td></tr>
<tr><td> </td><td><asp:DataGrid ID="dg" DataKeyField="EID" runat="server"
AutoGenerateColumns="false" AllowPaging="True" AllowSorting="True"
GridLines="Horizontal" onpageindexchanged="dg_PageIndexChanged"
PageSize="2" Width="100%" oncancelcommand="dg_CancelCommand"
ondeletecommand="dg_DeleteCommand" oneditcommand="dg_EditCommand"
onitemdatabound="dg_ItemDataBound" onupdatecommand="dg_UpdateCommand"
ShowFooter="True" >
<Columns>
<asp:TemplateColumn HeaderText="Sl No.">
<ItemTemplate><%#Container.ItemIndex+1 %></ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="EID">
<ItemTemplate><%#Eval("eid") %></ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="NAME">
<ItemTemplate><%#Eval("name") %></ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="tb3" runat="server" Text='<%#Eval("name") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="SALARY">
<ItemTemplate><asp:Label ID="lb9" runat="server" Text='<%#Eval("sal") %>'></asp:Label></ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="tb4" runat="server" Text='<%#Eval("SAL") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="UPDATE">
<ItemTemplate><asp:LinkButton ID="lb1" runat="server" CommandName="edit" Text="Edit"></asp:LinkButton></ItemTemplate>
<EditItemTemplate><asp:LinkButton ID="lb2" runat="server" CommandName="Cancel" Text="Cancel"></asp:LinkButton> <asp:LinkButton ID="lb3" runat="server" CommandName="update" Text="Update"></asp:LinkButton></EditItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="DELETE">
<ItemTemplate><asp:LinkButton ID="lb4" runat="server" CommandName="delete" Text="Delete"></asp:LinkButton></ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:DataGrid></td>
</table>
</ContentTemplate>
</asp:UpdatePanel>
</div>
</form>
</body>
</html>
The page looks like :
C# code :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DatabaseModel;
public partial class Default6 : System.Web.UI.Page
{
DatabaseEntities1 o = new DatabaseEntities1();
int i, j;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
view();
}
void view()
{
var m = from x in o.EMPs select x;
dg.DataSource = m;
dg.DataBind();
}
protected void dg_PageIndexChanged(object source, DataGridPageChangedEventArgs e)
{
dg.CurrentPageIndex = e.NewPageIndex;
view();
}
protected void dg_EditCommand(object source, DataGridCommandEventArgs e)
{
dg.EditItemIndex = e.Item.ItemIndex;
view();
}
protected void dg_CancelCommand(object source, DataGridCommandEventArgs e)
{
dg.EditItemIndex = -1;
view();
}
protected void dg_UpdateCommand(object source, DataGridCommandEventArgs e)
{
i = Convert.ToInt32(dg.DataKeys[e.Item.ItemIndex].ToString());
var m =( from x in o.EMPs where x.EID==i select x).First();
m.NAME = ((TextBox)dg.Items[e.Item.ItemIndex].FindControl("tb3")).Text;
m.SAL=Convert.ToInt32(((TextBox)dg.Items[e.Item.ItemIndex].FindControl("tb4")).Text);
o.SaveChanges();
lb.Text = "One record updated.";
dg.EditItemIndex = -1;
view();
}
protected void dg_DeleteCommand(object source, DataGridCommandEventArgs e)
{
i = Convert.ToInt32(dg.DataKeys[e.Item.ItemIndex].ToString());
var m = (from x in o.EMPs where x.EID == i select x).First();
o.EMPs.DeleteObject(m);
o.SaveChanges();
lb.Text = "One record delete.";
dg.CurrentPageIndex = 0;
view();
}
protected void dg_ItemDataBound(object sender, DataGridItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType==ListItemType.AlternatingItem)
{
j = j + Convert.ToInt32(((Label) e.Item.Cells[3].FindControl("lb9")).Text);
}
if (e.Item.ItemType == ListItemType.Footer)
{
e.Item.Cells[1].Text = "Total";
e.Item.Cells[3].Text = j.ToString();
}
}
protected void btn_Click(object sender, EventArgs e)
{
EMP p = new EMP();
p.EID = Convert.ToInt32(tb.Text);
p.NAME = tb1.Text;
p.SAL = Convert.ToInt32(tb2.Text);
o.EMPs.AddObject(p);
o.SaveChanges();
lb.Text = "One record saved.";
view();
}
}
No comments:
Post a Comment