动网论坛,站长建站首选,国内使用量最多的论坛软件 动网论坛官方技术讨论区 站长工具 申请属于您自己的免费论坛
首页 | 新闻资讯 | 网站运营 | 网络编程 | 数据库 | 服务器 | 网页设计 | 图像媒体 | 网络应用 | 搜索优化 | 资源下载 | 动网主机 | DVBOX
    本站内  互联网 ASP论坛  ASP.Net论坛  PHP论坛
  
   .Net → 阅读文章

 EditCommandColumn

作者来源: 
阅读 1313 人次 , 2006-3-29 4:30:00 


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, 表示没有任何项目被编辑,让数据回复到原来的显示模式。如下图所示:
 
 收藏本文  打印本文  论坛讨论  关闭窗口
· 上一篇:数据的更新
· 下一篇:HyperlinkColumn
· Unify的eWave ServletExec拒绝服务漏洞
· 用ASP.NET动态生成图像(转1)
· 变数
· 创建用于ASP.NET的分页程序控件
· Web 控件的使用


关于本站 | 联系我们 | 业务合作 | 客户案例 | 诚聘英才 | 广告合作 | 收藏本站
海口动网先锋网络科技有限公司版权所有
Copyright © 2000 - 2006 Cndw.Com
中华人民共和国电信与信息服务业务经营许可证编号 琼 ICP 020077