在上次给出了动态模板列的一个例子,网友问我更新操作和分页怎么做,下面给出代码。
前台:
<%@ page language=”c#” codebehind=”webform30.aspx.cs” autoeventwireup=”false” inherits=”csdn.webform30″ %>
<!doctype html public “-//w3c//dtd html 4.0 transitional//en” >
<html>
<head>
<title>webform30</title>
<meta name=”generator” content=”microsoft visual studio .net 7.1″>
<meta name=”code_language” content=”c#”>
<meta name=”vs_defaultclientscript” content=”javascript”>
<link href=”css.css” rel=”stylesheet” type=”text/css”>
<meta name=”vs_targetschema” content=”http://schemas.microsoft.com/intellisense/ie5″>
</head>
<body ms_positioning=”gridlayout”>
<form id=”form1″ method=”post” runat=”server”>
<asp:datagrid id=”datagrid1″ runat=”server”></asp:datagrid>
</form>
</body>
</html>
后台:
using system;
using system.collections;
using system.componentmodel;
using system.data;
using system.data.sqlclient;
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 csdn
{
/// <summary>
/// webform30 的摘要说明。
/// </summary>
public class webform30 : system.web.ui.page
{
protected system.web.ui.webcontrols.datagrid datagrid1;
private void page_load(object sender, system.eventargs e)
{
// 在此处放置用户代码以初始化页面
if(!ispostback)
{
bindgrid();
}
createdatagrid();//进行一些datagrid的设置
}
protected void createdatagrid()
{
datagrid1.autogeneratecolumns=false;//不启用自动生成列
datagrid1.cssclass=”border”;//边框样式
datagrid1.borderwidth=0;
datagrid1.cellspacing=1;
datagrid1.cellpadding=5;
datagrid1.itemstyle.cssclass=”item”;//普通列样式
datagrid1.headerstyle.cssclass=”header”;//头样式
datagrid1.pagerstyle.cssclass=”header”;//页脚样式
datagrid1.datakeyfield=”stuid”;//主键字段
datagrid1.allowpaging=true;//允许分页
datagrid1.pagesize=5;//分页大小
datagrid1.pagerstyle.mode=pagermode.numericpages;//数字形式分页
editcommandcolumn ecc=new editcommandcolumn();//更新按钮列
ecc.buttontype=buttoncolumntype.pushbutton;//下压按钮
ecc.edittext=”编辑”;
ecc.canceltext=”取消”;
ecc.updatetext=”更新”;//按钮文字
datagrid1.columns.add(ecc);//增加按钮列
datagrid1.editcommand+=new datagridcommandeventhandler(datagrid1_editcommand);
datagrid1.updatecommand+=new datagridcommandeventhandler(datagrid1_updatecommand);
datagrid1.cancelcommand+=new datagridcommandeventhandler(datagrid1_cancelcommand);//更新、取消、编辑事件注册
datagrid1.pageindexchanged += new system.web.ui.webcontrols.datagridpagechangedeventhandler(datagrid1_pageindexchanged);//分页事件注册,这里需要注意注册事件代码的位置,不能放到bindgrid()中
setbind(); //绑定数据
}
protected void bindgrid()
{
templatecolumn tm=new templatecolumn();
tm.itemtemplate=new columntemplate1();//普通列
tm.edititemtemplate=new columntemplate2();//编辑列
tm.headertext=”姓名”;
datagrid1.columns.addat(0,tm);//在第一列增加第一个模板列
templatecolumn tm2=new templatecolumn();
tm2.itemtemplate=new columntemplate3();
tm2.edititemtemplate=new columntemplate4();
tm2.headertext=”学院”;
datagrid1.columns.addat(1,tm2);//在第二列增加第一个模板列
datagrid1.itemdatabound += new system.web.ui.webcontrols.datagriditemeventhandler(datagrid1_itemdatabound);//数据绑定事件注册,这里需要注意注册事件代码的位置
setbind();
}
protected void setbind()
{
sqlconnection conn=new sqlconnection(system.configuration.configurationsettings.appsettings[“conn”]);
sqldataadapter da=new sqldataadapter(“select * from stu,dep where stu.studepid=dep.depid”,conn);
dataset ds=new dataset();
da.fill(ds,”table1″);
this.datagrid1.datasource=ds.tables[“table1”];
this.datagrid1.databind();
}
private void datagrid1_itemdatabound(object sender, system.web.ui.webcontrols.datagriditemeventargs e)
{
sqlconnection conn=new sqlconnection(system.configuration.configurationsettings.appsettings[“conn”]);
sqldataadapter da=new sqldataadapter(“select * from dep”,conn);
dataset ds=new dataset();
da.fill(ds,”table1″);
if(e.item.itemtype==listitemtype.edititem)
{
dropdownlist ddl=(dropdownlist)e.item.findcontrol(“dep”);
ddl.datasource=ds.tables[“table1”];
ddl.datatextfield=”depname”;
ddl.datavaluefield=”depid”;
ddl.databind();
ddl.items.findbyvalue(convert.tostring(databinder.eval(e.item.dataitem,”depid”))).selected=true;
}
}
private void datagrid1_editcommand(object source, system.web.ui.webcontrols.datagridcommandeventargs e)
{
this.datagrid1.edititemindex=e.item.itemindex;
bindgrid();
}
private void datagrid1_cancelcommand(object source, system.web.ui.webcontrols.datagridcommandeventargs e)
{
this.datagrid1.edititemindex=-1;
bindgrid();
}
private void datagrid1_updatecommand(object source, system.web.ui.webcontrols.datagridcommandeventargs e)
{
string uid=e.item.uniqueid+”:”;//注意别遗漏冒号
sqlconnection conn=new sqlconnection(system.configuration.configurationsettings.appsettings[“conn”]);
sqlcommand comm=new sqlcommand(“update stu set stuname=@name,studepid=@depid where stuid=@id”,conn);
sqlparameter parm1=new sqlparameter(“@name”,sqldbtype.nvarchar,50);
parm1.value=request.form[uid+”name”].tostring();
sqlparameter parm2=new sqlparameter(“@depid”,sqldbtype.int);
parm2.value=request.form[uid+”dep”].tostring();;
sqlparameter parm3=new sqlparameter(“@id”,sqldbtype.int);
parm3.value=this.datagrid1.datakeys[e.item.itemindex];
comm.parameters.add(parm1);
comm.parameters.add(parm2);
comm.parameters.add(parm3);
conn.open();
comm.executenonquery();
conn.close();
this.datagrid1.edititemindex=-1;
bindgrid();
//之所以不能采用以前的((textbox)e.item.findcontrol(“name”)).text来取得数据时因为,datagrid列是动态添加的,根本取不到
}
private void datagrid1_pageindexchanged(object source, system.web.ui.webcontrols.datagridpagechangedeventargs e)
{
this.datagrid1.currentpageindex=e.newpageindex;
bindgrid();
}
#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
}
public class columntemplate1 : itemplate
{
public void instantiatein(control container)
{
literalcontrol l = new literalcontrol();
l.databinding += new eventhandler(this.ondatabinding);
container.controls.add(l);
}
public void ondatabinding(object sender, eventargs e)
{
literalcontrol l = (literalcontrol) sender;
datagriditem container = (datagriditem) l.namingcontainer;
l.text = ((datarowview)container.dataitem)[“stuname”].tostring();
}
}
public class columntemplate2 : itemplate
{
public void instantiatein(control container)
{
textbox t = new textbox();
t.width=88;
t.id=”name”;//需要给一个id,在request.form的时候可以取
t.databinding += new eventhandler(this.ondatabinding);
container.controls.add(t);
}
public void ondatabinding(object sender, eventargs e)
{
textbox t= (textbox) sender;
datagriditem container = (datagriditem) t.namingcontainer;
t.text = ((datarowview)container.dataitem)[“stuname”].tostring();//绑定stuname字段
}
}
public class columntemplate3 : itemplate
{
public void instantiatein(control container)
{
literalcontrol l = new literalcontrol();
l.databinding += new eventhandler(this.ondatabinding);
container.controls.add(l);
}
public void ondatabinding(object sender, eventargs e)
{
literalcontrol l = (literalcontrol) sender;
datagriditem container = (datagriditem) l.namingcontainer;
l.text = ((datarowview)container.dataitem)[“depname”].tostring();
}
}
public class columntemplate4 : itemplate
{
public void instantiatein(control container)
{
dropdownlist dpl = new dropdownlist();
dpl.id=”dep”;
container.controls.add(dpl);
}//这里没有为这个下拉框进行数据绑定,在datagrid1的itemdatabound中进行了这个操作
}
}
代码比较简单,在代码中有注释简单的解释。
补充2点:(1)这次的datagrid是在设计生成的,不是上次的运行时,后者会造成更新的紊乱;(2)处于简单,代码中模板列类没有很好的封装,在运用的时候还是应该对字段和模板列类型(listitemtype)进行封装