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

 数据的编辑

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


下列两个范例都是将datalist web 控件加入编辑数据的功能,第一个范例只用onitemcommand 事件,第二个范例综合oneditcommand、oncancelcommand,以及onupdatecommand 事件。使用者在选择一个项目展开selecteditemtemplate 样版显示详细资料后,若选择「编辑」选项时会显示edititemtemplate 样版,edititemtemplate 样版是以textbox来显示使用者资料,并且可以接受使用者的修改。修改完成后可以按「确定」执行将数据更新回数据源的动作,「放弃」可以回到显示使用者详细数据的选项:

范例一只使用onitemcommand 事件

<%@import namespace=system.data.ado%>
<%@import namespace=system.data%>
<html>
<form runat="server">
<asp:datalist id="dla" onitemcommand="dla_icmd"
gridlines="both" runat="server">
<template name="itemtemplate">
<asp:image imageurl="ico1.gif" runat="server"/>
姓名: <%#container.dataitem("username")%>
<asp:linkbutton id="lbshow" text=">" runat="server"/>
</template>
<template name="edititemtemplate">
<asp:image imageurl="ico1.gif" runat="server"/>
姓名: <%#container.dataitem("username")%><br>
电话:<asp:textbox id="t1"
text='<%#container.dataitem("usertel")%>'
runat="server"/><br>
住址:<asp:textbox id="t2"
text='<%#container.dataitem("useradd")%>'
runat="server"/><br>
电邮:<asp:textbox id="t3"
text='<%#container.dataitem("useremail")%>'
runat="server"/><br>
<asp:linkbutton id="lbcancel" text="[放弃]" runat="server"/>
<asp:linkbutton id="lbsubmit" text="[确定]" runat="server"/><br>
</template>
<template name="selecteditemtemplate">
<asp:image imageurl="ico1.gif" runat="server"/>
姓名: <%#container.dataitem("username")%>
<asp:linkbutton id="lbclose" text="<" runat="server"/><br>
电话: <%#container.dataitem("usertel")%><br>
住址: <%#container.dataitem("useradd")%><br>
电邮: <%#container.dataitem("useremail")%><br>
<asp:linkbutton id="lbedit" text="[编辑]" runat="server"/><br>
</template>
</asp:datalist>
</form>
<script language="vb" runat="server">
dim dsca as adodatasetcommand=new adodatasetcommand("select * from
members", _
"provider=microsoft.jet.oledb.4.0;data
source=c:\inetpub\wwwroot\cr\ch05\myweb.mdb")
dim dsdataset as dataset=new dataset
dim dtdatatable as datatable
sub page_load(sender as object, e as eventargs)
dsca.filldataset(dsdataset,"members")
dtdatatable=dsdataset.tables("members")
dla.datasource=dtdatatable.defaultview
if not page.ispostback then page.databind()
end sub
sub dla_icmd(sender as object, e as datalistcommandeventargs)
if e.commandsource.id="lbshow" then
dla.selectedindex=e.item.itemindex
elseif e.commandsource.id="lbclose" then
dla.selectedindex=-1
elseif e.commandsource.id="lbedit" then
dla.edititemindex=e.item.itemindex
elseif e.commandsource.id="lbcancel" then
dla.edititemindex=-1
elseif e.commandsource.id="lbsubmit" then
dim txttemp as textbox
txttemp=e.item.findcontrol("t1") '取回listitem 中名为t1 的控件参

dtdatatable.rows(dla.edititemindex)("usertel")=txttemp.text
txttemp=e.item.findcontrol("t2") '取回listitem 中名为t2 的控件参

dtdatatable.rows(dla.edititemindex)("useradd")=txttemp.text
txttemp=e.item.findcontrol("t3") '取回listitem 中名为t3 的控件参

dtdatatable.rows(dla.edititemindex)("useremail")=txttemp.text
dsca.update(dsdataset,"members")
dla.edititemindex=-1
end if
dla.databind()
end sub
</script>
</html>


  由于我们要在许多程序中使用datatable、dataset 以及datasetcommand 对象,所我们将这些对象变量宣告在网页阶层的宣告区。程序开始执行时,先以itemtemplate 样版来显示资料。待任意项目被选择后,便以selecteditemtemplate 样版来显示该项目,如下图所示:


选择「编辑」选项后,便以edititemtemplate 样版来显示所要编辑的记录;使用者可以在textbox中编修数据,如下图所示:


  待使用者将数据编辑完毕点选「确定」按钮时,我们就在dla_icmd 事件程序中将使用者所作的修改更新回数据源;如下程序代码片段所示:

elseif e.commandsource.id="lbsubmit" then
dim txttemp as textbox
txttemp=e.item.findcontrol("t1") '取回listitem 中名为t1 的控件参

dtdatatable.rows(dla.edititemindex)("usertel")=txttemp.text
txttemp=e.item.findcontrol("t2") '取回listitem 中名为t2 的控件参

dtdatatable.rows(dla.edititemindex)("useradd")=txttemp.text
txttemp=e.item.findcontrol("t3") '取回listitem 中名为t3 的控件参

then
dtdatatable.rows(dla.edititemindex)("useremail")=txttemp.text
dsca.update(dsdataset,"members")
dla.edititemindex=-1
end if

上述程序代码片段宣告一个textbox 型态的对象变量,用来存放listitem 中textbox 控件的参考;此时我们就可以用listitem 对象的findcontrol() 方法来取得指定的控件参考,findcontrol只要传入想要传回对象参考的id 属性即可。最后只要呼叫datasetcommand 对象的update 方法,就可以将使用者所作的修改更新回原来的数据源。所以选择「确定」回到selecteditemtanplate 样版的模式来显示数据时,就可以看到资料已经被更新了;如下图所示:


范例二综合相关事件

<%@import namespace=system.data.ado%>
<%@import namespace=system.data%>
<html>
<form runat="server">
<asp:datalist id="dla" onitemcommand="dla_icmd"
oneditcommand="dla_ecmd"
oncancelcommand="dla_ccmd" gridlines="both"
runat="server">
<template name="itemtemplate">
<asp:image imageurl="ico1.gif" runat="server"/>
姓名: <%#container.dataitem("username")%>
<asp:linkbutton id="lbshow" text=">" commandname="show"
runat="server"/>
</template>
<template name="edititemtemplate">
<asp:image imageurl="ico1.gif" runat="server"/>
姓名: <%#container.dataitem("username")%><br>
电话:<asp:textbox id="t1"
text='<%#container.dataitem("usertel")%>'
runat="server"/><br>
住址:<asp:textbox id="t2"
text='<%#container.dataitem("useradd")%>'
runat="server"/><br>
电邮:<asp:textbox id="t3"
text='<%#container.dataitem("useremail")%>'
runat="server"/><br>
<asp:linkbutton id="lbcancel" text="[放弃]" commandname="cancel"
runat="server"/>
<asp:linkbutton id="lbsubmit" text="[确定]" commandname="submit"
runat="server"/><br>
</template>
<template name="selecteditemtemplate">
<asp:image imageurl="ico1.gif" runat="server"/>
姓名: <%#container.dataitem("username")%>
<asp:linkbutton id="lbclose" text="<" commandname="close"
runat="server"/><br>
电话: <%#container.dataitem("usertel")%><br>
住址: <%#container.dataitem("useradd")%><br>
电邮: <%#container.dataitem("useremail")%><br>
<asp:linkbutton id="lbedit" text="[编辑]" commandname="edit"
runat="server"/><br>
</template>
</asp:datalist>
</form>
<script language="vb" runat="server">
dim dsca as adodatasetcommand=new adodatasetcommand("select * from
members", _
"provider=microsoft.jet.oledb.4.0;data
source=c:\inetpub\wwwroot\cr\ch05\myweb.mdb")
dim dsdataset as dataset=new dataset
dim dtdatatable as datatable
sub page_load(sender as object, e as eventargs)
dsca.filldataset(dsdataset,"members")
dtdatatable=dsdataset.tables("members")
dla.datasource=dtdatatable.defaultview
if not page.ispostback then page.databind()
end sub
sub dla_icmd(sender as object, e as datalistcommandeventargs)
if e.commandsource.commandname="show" then
dla.selectedindex=e.item.itemindex
elseif e.commandsource.commandname="close" then
dla.selectedindex=-1
elseif e.commandsource.commandname="submit" then
dim txttemp as textbox
txttemp=e.item.findcontrol("t1") '取回listitem 中名为t1 的控件参

dtdatatable.rows(dla.edititemindex)("usertel")=txttemp.text
txttemp=e.item.findcontrol("t2") '取回listitem 中名为t2 的控件参

dtdatatable.rows(dla.edititemindex)("useradd")=txttemp.text
txttemp=e.item.findcontrol("t3") '取回listitem 中名为t3 的控件参

dtdatatable.rows(dla.edititemindex)("useremail")=txttemp.text
dsca.update(dsdataset,"members")
dla.edititemindex=-1
end if
dla.databind()
end sub
sub dla_ecmd(sender as object, e as datalistcommandeventargs)
dla.edititemindex=e.item.itemindex
dla.databind()
end sub
sub dla_ccmd(sender as object, e as datalistcommandeventargs)
dla.edititemindex=-1
dla.databind()
end sub
</script>
</html>

上述范例二的执行结果和范例一完全一样。
 
 收藏本文  打印本文  论坛讨论  关闭窗口
· 上一篇:DataGrid Web 控件
· 下一篇:DataList Web--OnItemCommand 事件
· ASP.NET 入门的五个步骤
· 给Asp.Net初学者的关于继承和多态性的例子
· Calendar Web 控件的事件
· 全面剖析VB.NET(7)
· ASP.NET升级能力探讨(二)


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