现在数据库中建立test表,字段分别为id、code、name;
数据库的链接这里就不具体写了,数据库的链接和操作请求我写在了areainfo.cs中,代码中会用到,这里声明一下
<%@ page language=”c#” codebehind=”update.aspx.cs” autoeventwireup=”false” inherits=”client.page.areainfo.update” %>
<!doctype html public “-//w3c//dtd html 4.0 transitional//en” >
<html>
<head>
<title>test</title>
<meta name=”generator” content=”microsoft visual studio .net 7.1″>
<meta name=”code_language” content=”c#”>
<meta name=”vs_defaultclientscript” content=”javascript”>
<meta name=”vs_targetschema” content=”http://schemas.microsoft.com/intellisense/ie5“>
</head>
<body ms_positioning=”gridlayout” bgcolor=”ivory”>
<font face=”arial” size=”3″></font><font style=”background-color: #ffffff” face=”arial”>
</font>
<br>
<form runat=”server” id=”form1″>
<table id=”table1″ cellspacing=”0″ bgcolor=”#6699cc” cellpadding=”1″ width=”300″ border=”1″>
<tr>
<td><b>test</b>
</td>
</tr>
<td>
<asp:datagrid id=”dg1″ runat=”server” bordercolor=”green” width=”640″ pagesize=”5″ allowpaging=”true”
onpageindexchanged=”page_grid” borderwidth=”1″ cellpadding=”3″ autogeneratecolumns=”false”
showheader=”true” visible=”true” oneditcommand=”people_edit” oncancelcommand=”people_cancel”
onupdatecommand=”people_update” ondeletecommand=”people_delete”>
<headerstyle bordercolor=”white” backcolor=”black” forecolor=”white” font-bold=”true” font-name=”arial”
font-size=”9″ horizontalalign=”center” />
<itemstyle bordercolor=”” backcolor=”#fffff0″ forecolor=”black” font-name=”arial” font-size=”8″
font-bold=”false” horizontalalign=”center” />
<edititemstyle bordercolor=”” backcolor=”#fffff0″ forecolor=”black” font-name=”arial” font-size=”7″
font-bold=”false” horizontalalign=”center” />
<pagerstyle mode=”numericpages” font-size=”8″ />
<columns>
<asp:boundcolumn headertext=”id” readonly=”true” datafield=”id” />
<asp:templatecolumn>
<headertemplate>
<b>代码</b>
</headertemplate>
<itemtemplate>
<asp:label text=<%# databinder.eval(container.dataitem, “code”).tostring().trim() %> runat=”server” id=”label1″ name=”label1″/>
</itemtemplate>
<edititemtemplate>
<asp:textbox id=”code” text=<%# databinder.eval(container.dataitem, “code”).tostring().trim() %> runat=”server” width=”100%”/>
</edititemtemplate>
</asp:templatecolumn>
<asp:templatecolumn>
<headertemplate>
<b>名称</b>
</headertemplate>
<itemtemplate>
<asp:label width=”200″ text=<%# databinder.eval(container.dataitem, “name”).tostring().trim() %> runat=”server” id=”label2″ name=”label2″/>
</itemtemplate>
<edititemtemplate>
<asp:textbox id=”name” text=<%# databinder.eval(container.dataitem, “name”).tostring().trim() %> runat=”server”/>
</edititemtemplate>
</asp:templatecolumn>
<asp:editcommandcolumn buttontype=”pushbutton” canceltext=”取消” edittext=”修改” updatetext=”更新” />
<asp:buttoncolumn text=”删除” commandname=”delete” buttontype=”pushbutton”></asp:buttoncolumn>
</columns>
</asp:datagrid></td>
</tr>
</table>
</form>
</body>
</html>
update.aspx.cs
using system;
using system.collections;
using system.componentmodel;
using system.data;
using system.drawing;
using system.web;
using system.web.sessionstate;
using system.web.ui;
using system.web.ui.webcontrols;
using system.web.ui.htmlcontrols;
namespace client.page.areainfo
{
/// <summary>
/// update 的摘要说明。
/// </summary>
public class update : system.web.ui.page
{
protected system.web.ui.webcontrols.datagrid dg1;
protected classes.areainfo myclass=new client.classes.areainfo();
private void page_load(object sender, system.eventargs e)
{
if(!this.ispostback)
{
this.dg1.datasource=myclass.bindgrid();
this.dg1.databind();
}
// 在此处放置用户代码以初始化页面
}
public void page_grid(object sender, datagridpagechangedeventargs e)
{
dg1.currentpageindex = e.newpageindex;
dg1.datasource =myclass.bindgrid();
dg1.databind();
}
public void dg1_edit(object sender, datagridcommandeventargs e)
{
dg1.edititemindex = e.item.itemindex;
dg1.datasource =myclass.bindgrid();
dg1.databind();
}
public void dg1_cancel(object sender, datagridcommandeventargs e)
{
dg1.edititemindex = -1;
dg1.datasource = myclass.bindgrid();
dg1.databind();
}
public void dg1_update(object sender, datagridcommandeventargs e)
{
string code = ((textbox)e.item.cells[1].controls[1]).text;
string name = ((textbox)e.item.cells[2].controls[1]).text;
int id=int.parse(e.item.cells[0].text);
myclass.updatearea(id,code,name);
dg1.edititemindex = -1;
dg1.datasource = myclass.bindgrid();
dg1.databind();
}
public void dg1_delete(object sender, datagridcommandeventargs e)
{
int id=int.parse(e.item.cells[0].text);
myclass.deletearea(id);
this.dg1.datasource=myclass.bindgrid();
this.dg1.databind();
}
#region web 窗体设计器生成的代码
override protected void oninit(eventargs e)
{
//
// codegen: 该调用是 asp.net web 窗体设计器所必需的。
//
initializecomponent();
base.oninit(e);
}
/// <summary>
/// 设计器支持所需的方法 – 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void initializecomponent()
{
this.load += new system.eventhandler(this.page_load);
}
#endregion
}
}