Design the page :
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default3.aspx.cs" Inherits="Default3" %>
<!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">
<div>
<center>
<asp:ListView ID="lv" runat="server" ItemPlaceholderID="x" DataKeyNames="eid"
onitemediting="lv_ItemEditing" onitemcanceling="lv_ItemCanceling"
onitemdeleting="lv_ItemDeleting" onitemupdating="lv_ItemUpdating" oniteminserting="lv_ItemInserting"
onsorting="lv_Sorting" InsertItemPosition="FirstItem">
<LayoutTemplate>
<table align="center" width="300" border="1">
<tr><th><asp:LinkButton ID="lb8" CommandName="sort" runat="server" CommandArgument="eid" Text="EID" Font-Underline="false"></asp:LinkButton></th><th><asp:LinkButton ID="LinkButton1" runat="server" CommandName="sort" Text="NAME" CommandArgument="name" Font-Underline="false"></asp:LinkButton></th><th>EDIT</th><th>DELETE</th></tr>
<tr id="x" runat="server" ></tr>
</table>
</LayoutTemplate>
<EmptyDataTemplate>
<b>Sorry records not avalible.</b>
</EmptyDataTemplate>
<ItemTemplate>
<tr><td align="center"><%#Eval("EID") %></td><td align="left"><%#Eval("NAME") %></td><td><asp:LinkButton ID="lb" runat="server" Text="Edit" CommandName="edit" Font-Underline="false"></asp:LinkButton></td><td><asp:LinkButton ID="lb1" runat="server" Text="Delete" CommandName="delete" Font-Underline="false"></asp:LinkButton></td></tr>
</ItemTemplate>
<EditItemTemplate>
<tr><td><%#Eval("EID") %></td><td><asp:TextBox ID="tb" runat="server" Text='<%#Eval("name") %>'></asp:TextBox></td><td><asp:LinkButton ID="lb2" runat="server" Text="Update" CommandName="update" Font-Underline="false"></asp:LinkButton> <asp:LinkButton ID="lb4" runat="server" Text="Cancel" CommandName="Cancel" Font-Underline="false"></asp:LinkButton></td><td> </td></tr>
</EditItemTemplate>
<InsertItemTemplate>
<tr><td><asp:TextBox ID="tb1" runat="server"></asp:TextBox></td><td><asp:TextBox ID="tb2" runat="server"></asp:TextBox></td><td colspan="2" align="center"><asp:LinkButton ID="lb5" runat="server" Text="Save" CommandName="insert" Font-Underline="false"></asp:LinkButton></td></tr>
</InsertItemTemplate>
</asp:ListView>
<asp:DataPager runat="server" ID="ContactsDataPager" PageSize="2" PagedControlID="lv" OnPreRender="y">
<Fields>
<asp:NextPreviousPagerField ButtonType="Button"
ShowFirstPageButton="true"
ShowLastPageButton="true" />
</Fields>
</asp:DataPager>
</center>
</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 Default3 : System.Web.UI.Page
{
DatabaseEntities o = new DatabaseEntities();
int i;
string ss;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
view(2, "eid");
}
}
void view(int j,string s)
{
if (j == 1)
{
if (s == "eid")
{
var mm = (from x in o.EMPs orderby x.EID ascending select x);
lv.DataSource = mm;
lv.DataBind();
}
else
{
var mm = (from x in o.EMPs orderby x.NAME ascending select x);
lv.DataSource = mm;
lv.DataBind();
}
}
else
{
if (s == "eid")
{
var mm = (from x in o.EMPs orderby x.EID descending select x);
lv.DataSource = mm;
lv.DataBind();
}
else
{
var mm = (from x in o.EMPs orderby x.NAME descending select x);
lv.DataSource = mm;
lv.DataBind();
}
}
}
protected void lv_ItemEditing(object sender, ListViewEditEventArgs e)
{
lv.EditIndex = e.NewEditIndex;
view(2, "eid");
}
protected void lv_ItemCanceling(object sender, ListViewCancelEventArgs e)
{
lv.EditIndex = -1;
view(2, "eid");
}
protected void lv_ItemUpdating(object sender, ListViewUpdateEventArgs e)
{
i=Convert.ToInt32(lv.DataKeys[e.ItemIndex].Value);
var m = (from x in o.EMPs where x.EID == i select x).First();
m.NAME = ((TextBox)lv.Items[e.ItemIndex].FindControl("tb")).Text;
o.SaveChanges();
lv.EditIndex = -1;
view(2, "eid");
}
protected void lv_ItemDeleting(object sender, ListViewDeleteEventArgs e)
{
i = Convert.ToInt32(lv.DataKeys[e.ItemIndex].Value);
var m = (from x in o.EMPs where x.EID == i select x).First();
o.EMPs.DeleteObject(m);
o.SaveChanges();
view(2, "eid");
}
protected void lv_ItemInserting(object sender, ListViewInsertEventArgs e)
{
EMP p = new EMP();
p.EID = Convert.ToInt32(((TextBox)lv.InsertItem.FindControl("tb1")).Text);
p.NAME = ((TextBox)lv.InsertItem.FindControl("tb2")).Text;
o.EMPs.AddObject(p);
o.SaveChanges();
view(2, "eid");
}
protected void lv_Sorting(object sender, ListViewSortEventArgs e)
{
ss = e.SortExpression.ToString();
if (ListViewSortDirection==SortDirection.Ascending)
{
view(1, ss);
ListViewSortDirection = SortDirection.Descending;
}
else
{
view(2, ss);
ListViewSortDirection = SortDirection.Ascending;
}
}
protected SortDirection ListViewSortDirection
{
get
{
if (ViewState["sortDirection"] == null)
ViewState["sortDirection"] = SortDirection.Ascending;
return (SortDirection)ViewState["sortDirection"];
}
set { ViewState["sortDirection"] = value; }
}
protected void y(object sender, EventArgs e)
{
view(1, "eid");
}
}
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default3.aspx.cs" Inherits="Default3" %>
<!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">
<div>
<center>
<asp:ListView ID="lv" runat="server" ItemPlaceholderID="x" DataKeyNames="eid"
onitemediting="lv_ItemEditing" onitemcanceling="lv_ItemCanceling"
onitemdeleting="lv_ItemDeleting" onitemupdating="lv_ItemUpdating" oniteminserting="lv_ItemInserting"
onsorting="lv_Sorting" InsertItemPosition="FirstItem">
<LayoutTemplate>
<table align="center" width="300" border="1">
<tr><th><asp:LinkButton ID="lb8" CommandName="sort" runat="server" CommandArgument="eid" Text="EID" Font-Underline="false"></asp:LinkButton></th><th><asp:LinkButton ID="LinkButton1" runat="server" CommandName="sort" Text="NAME" CommandArgument="name" Font-Underline="false"></asp:LinkButton></th><th>EDIT</th><th>DELETE</th></tr>
<tr id="x" runat="server" ></tr>
</table>
</LayoutTemplate>
<EmptyDataTemplate>
<b>Sorry records not avalible.</b>
</EmptyDataTemplate>
<ItemTemplate>
<tr><td align="center"><%#Eval("EID") %></td><td align="left"><%#Eval("NAME") %></td><td><asp:LinkButton ID="lb" runat="server" Text="Edit" CommandName="edit" Font-Underline="false"></asp:LinkButton></td><td><asp:LinkButton ID="lb1" runat="server" Text="Delete" CommandName="delete" Font-Underline="false"></asp:LinkButton></td></tr>
</ItemTemplate>
<EditItemTemplate>
<tr><td><%#Eval("EID") %></td><td><asp:TextBox ID="tb" runat="server" Text='<%#Eval("name") %>'></asp:TextBox></td><td><asp:LinkButton ID="lb2" runat="server" Text="Update" CommandName="update" Font-Underline="false"></asp:LinkButton> <asp:LinkButton ID="lb4" runat="server" Text="Cancel" CommandName="Cancel" Font-Underline="false"></asp:LinkButton></td><td> </td></tr>
</EditItemTemplate>
<InsertItemTemplate>
<tr><td><asp:TextBox ID="tb1" runat="server"></asp:TextBox></td><td><asp:TextBox ID="tb2" runat="server"></asp:TextBox></td><td colspan="2" align="center"><asp:LinkButton ID="lb5" runat="server" Text="Save" CommandName="insert" Font-Underline="false"></asp:LinkButton></td></tr>
</InsertItemTemplate>
</asp:ListView>
<asp:DataPager runat="server" ID="ContactsDataPager" PageSize="2" PagedControlID="lv" OnPreRender="y">
<Fields>
<asp:NextPreviousPagerField ButtonType="Button"
ShowFirstPageButton="true"
ShowLastPageButton="true" />
</Fields>
</asp:DataPager>
</center>
</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 Default3 : System.Web.UI.Page
{
DatabaseEntities o = new DatabaseEntities();
int i;
string ss;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
view(2, "eid");
}
}
void view(int j,string s)
{
if (j == 1)
{
if (s == "eid")
{
var mm = (from x in o.EMPs orderby x.EID ascending select x);
lv.DataSource = mm;
lv.DataBind();
}
else
{
var mm = (from x in o.EMPs orderby x.NAME ascending select x);
lv.DataSource = mm;
lv.DataBind();
}
}
else
{
if (s == "eid")
{
var mm = (from x in o.EMPs orderby x.EID descending select x);
lv.DataSource = mm;
lv.DataBind();
}
else
{
var mm = (from x in o.EMPs orderby x.NAME descending select x);
lv.DataSource = mm;
lv.DataBind();
}
}
}
protected void lv_ItemEditing(object sender, ListViewEditEventArgs e)
{
lv.EditIndex = e.NewEditIndex;
view(2, "eid");
}
protected void lv_ItemCanceling(object sender, ListViewCancelEventArgs e)
{
lv.EditIndex = -1;
view(2, "eid");
}
protected void lv_ItemUpdating(object sender, ListViewUpdateEventArgs e)
{
i=Convert.ToInt32(lv.DataKeys[e.ItemIndex].Value);
var m = (from x in o.EMPs where x.EID == i select x).First();
m.NAME = ((TextBox)lv.Items[e.ItemIndex].FindControl("tb")).Text;
o.SaveChanges();
lv.EditIndex = -1;
view(2, "eid");
}
protected void lv_ItemDeleting(object sender, ListViewDeleteEventArgs e)
{
i = Convert.ToInt32(lv.DataKeys[e.ItemIndex].Value);
var m = (from x in o.EMPs where x.EID == i select x).First();
o.EMPs.DeleteObject(m);
o.SaveChanges();
view(2, "eid");
}
protected void lv_ItemInserting(object sender, ListViewInsertEventArgs e)
{
EMP p = new EMP();
p.EID = Convert.ToInt32(((TextBox)lv.InsertItem.FindControl("tb1")).Text);
p.NAME = ((TextBox)lv.InsertItem.FindControl("tb2")).Text;
o.EMPs.AddObject(p);
o.SaveChanges();
view(2, "eid");
}
protected void lv_Sorting(object sender, ListViewSortEventArgs e)
{
ss = e.SortExpression.ToString();
if (ListViewSortDirection==SortDirection.Ascending)
{
view(1, ss);
ListViewSortDirection = SortDirection.Descending;
}
else
{
view(2, ss);
ListViewSortDirection = SortDirection.Ascending;
}
}
protected SortDirection ListViewSortDirection
{
get
{
if (ViewState["sortDirection"] == null)
ViewState["sortDirection"] = SortDirection.Ascending;
return (SortDirection)ViewState["sortDirection"];
}
set { ViewState["sortDirection"] = value; }
}
protected void y(object sender, EventArgs e)
{
view(1, "eid");
}
}
No comments:
Post a Comment