Design code:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!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 align="center">
<asp:DetailsView ID="dv" runat="server" AutoGenerateRows="false" DefaultMode="Edit"
onitemupdating="dv_ItemUpdating" AllowPaging="True"
onpageindexchanging="dv_PageIndexChanging" >
<Fields>
<asp:TemplateField HeaderText="ID">
<ItemTemplate>
<asp:Label ID="lb" runat="server" Text='<%#Eval("id") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:Label ID="lb1" runat="server" Text='<%#Eval("id") %>'></asp:Label>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Name">
<ItemTemplate>
<asp:Label ID="lb3" runat="server" Text='<%#Eval("name") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="tb" runat="server" Text='<%#Eval("name") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:CommandField ShowEditButton="true" CancelText="" EditText="Update" />
</Fields>
</asp:DetailsView>
</div>
</form>
</body>
</html>
c# code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class _Default : System.Web.UI.Page
{
DataClassesDataContext o = new DataClassesDataContext();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
fill();
}
}
void fill()
{
dv.DataSource = o.EMPs.ToList();
dv.DataBind();
}
protected void dv_ItemUpdating(object sender, DetailsViewUpdateEventArgs e)
{
o.EMPs.Where(b => b.ID == Convert.ToInt32(((Label)dv.Rows[0].Cells[1].FindControl("lb1")).Text)).First().NAME = ((TextBox)dv.Rows[1].Cells[1].FindControl("tb")).Text;
o.SubmitChanges();
Response.Write("<script>alert('one record updated.')</script>");
fill();
}
protected void dv_PageIndexChanging(object sender, DetailsViewPageEventArgs e)
{
dv.PageIndex = e.NewPageIndex;
fill();
}
}
page looks like:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!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 align="center">
<asp:DetailsView ID="dv" runat="server" AutoGenerateRows="false" DefaultMode="Edit"
onitemupdating="dv_ItemUpdating" AllowPaging="True"
onpageindexchanging="dv_PageIndexChanging" >
<Fields>
<asp:TemplateField HeaderText="ID">
<ItemTemplate>
<asp:Label ID="lb" runat="server" Text='<%#Eval("id") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:Label ID="lb1" runat="server" Text='<%#Eval("id") %>'></asp:Label>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Name">
<ItemTemplate>
<asp:Label ID="lb3" runat="server" Text='<%#Eval("name") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="tb" runat="server" Text='<%#Eval("name") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:CommandField ShowEditButton="true" CancelText="" EditText="Update" />
</Fields>
</asp:DetailsView>
</div>
</form>
</body>
</html>
c# code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class _Default : System.Web.UI.Page
{
DataClassesDataContext o = new DataClassesDataContext();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
fill();
}
}
void fill()
{
dv.DataSource = o.EMPs.ToList();
dv.DataBind();
}
protected void dv_ItemUpdating(object sender, DetailsViewUpdateEventArgs e)
{
o.EMPs.Where(b => b.ID == Convert.ToInt32(((Label)dv.Rows[0].Cells[1].FindControl("lb1")).Text)).First().NAME = ((TextBox)dv.Rows[1].Cells[1].FindControl("tb")).Text;
o.SubmitChanges();
Response.Write("<script>alert('one record updated.')</script>");
fill();
}
protected void dv_PageIndexChanging(object sender, DetailsViewPageEventArgs e)
{
dv.PageIndex = e.NewPageIndex;
fill();
}
}
page looks like:
No comments:
Post a Comment