editcommandcolumn 最主要的功能是利用linkbutton 或pushbutton 来下达编辑资料的命令,
并且可以触发datagrid web 控件的事件。editcommandcolumn 进入编辑模式的时候会产生
textbox 让使用者编辑,也可以和templatecolumn 一起配合使用,我们后面介绍
templatecolumn 的时候会说明。其使用语法如下所示:
<asp:editcommandcolumn
buttontype="linkbutton | pushbutton"
canceltext="cancelbuttoncaption"
edittext="editbuttoncaption"
footertext="footertext"
headerimageurl="url"
headertext="headertext"
readonly="true | false"
sortfield="datasourcefieldtosortby"
updatetext="updatebuttoncaption"
visible="true | false"
/>
其中除了共同基础属性以及样式对象外,常用的属性如下表所示:

当使用者点选了显示edittext 的控件时,datagrid web 控件会自动触发oneditcommand 事件,
并执行oneditcommand 属性所指定的事件程序;倘若点选了显示updatetext 以及canceltext
内容的控制像,也一样分别自动触发onupdatecommand 以及oncancelcommand 事件。下
列程序代码范例增加了一个editcommandcolumn 字段,并且显示使用者点选了哪一个按钮:
<%@import namespace=system.data.ado%>
<%@import namespace=system.data%>
<!--#include file="gettable.inc"-->
<html>
<form runat="server">
<asp:datagrid id="dga" allowpaging="true" pagesize="5"
onpageindexchanged="dga_pagechg" runat="server"
pagerstyle-mode="numericpages" bordercolor="#808080"
headerstyle-font-names="courier new"
headerstyle-backcolor="#d1dceb"
headerstyle-font-bold="true" headerstyle-horizontalalign="center"
oneditcommand="dga_ecmd" onupdatecommand="dga_ucmd"
oncancelcommand="dga_ccmd" >
<property name="columns">
<asp:boundcolumn
headertext="姓名"
datafield="username"/>
<asp:boundcolumn
headertext="电话"
datafield="usertel"/>
<asp:editcommandcolumn
headertext="编辑"
buttontype="pushbutton"
edittext="编辑"
updatetext="更新"
canceltext="放弃"/>
</property>
</asp:datagrid>
</form>
<asp:label id="label1" runat="server"/>
<script language="vb" runat="server">
dim dtdatatable as datatable=gettable("ch08\myweb.mdb", "members")
sub page_load(sender as object, e as eventargs)
if page.ispostback=false then
dga.datasource=dtdatatable.defaultview
page.databind()
end if
end sub
sub dga_pagechg(sender as object, e as datagridpagechangedeventargs)
dga.datasource=dtdatatable.defaultview
page.databind()
end sub
sub dga_ecmd(sender as object, e as datagridcommandeventargs)
label1.text="您点选了第" & (e.item.itemindex+1).tostring & " 个字
段的编辑."
dga.edititemindex=e.item.itemindex
dga.datasource=dtdatatable.defaultview
page.databind()
end sub
sub dga_ucmd(sender as object, e as datagridcommandeventargs)
label1.text="您点选了第" & (e.item.itemindex+1).tostring & " 个字
段的更新."
dga.edititemindex=-1
dga.datasource=dtdatatable.defaultview
page.databind()
end sub
sub dga_ccmd(sender as object, e as datagridcommandeventargs)
label1.text="您点选了第" & (e.item.itemindex+1).tostring & " 个字
段的放弃."
dga.edititemindex=-1
dga.datasource=dtdatatable.defaultview
page.databind()
end sub
</script>
</html>
上述范例中,由于datatable 对象要让其它事件程序使用,所以我们在网页阶层的宣告区宣告。
我们也指定了oneditcommand 事件、onupdatecommand 事件以及oncancelcommand 事
件;当使用者按下「编辑」按钮时会自动触发我们所指定的dga_ecmd 事件程序,如下程序代
码范例所示:
sub dga_ecmd(sender as object, e as datagridcommandeventargs)
label1.text="您点选了第" & (e.item.itemindex+1).tostring & " 个字
段的编辑."
dga.edititemindex=e.item.itemindex
dga.datasource=dtdatatable.defaultview
page.databind()
end sub
上述程序代码片段将datagrid 的edititemindex 属性设定为使用者所点选记录的索引值,所以
该笔记录就会进入编辑模式;此时原来字段内的「编辑」按钮就会变成「更新」以及「放弃」。
使用者若编辑完毕后再点选「更新」或「放弃」按钮时,即触发所指定的onupdatecommand
或是oncancelcommand 事件;这些事件最后将datagrid 对象的edititemindex 属性设为-1,
表示没有任何项目被编辑,让数据回复到原来的显示模式。如下图所示:
