加入收藏 | 设为首页 | 会员中心 | 我要投稿 源码网 (https://www.900php.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > MsSql教程 > 正文

在ASP.NET 2.0中操作数据之三十六:在DataList里编辑和删除数据

发布时间:2016-11-23 22:17:20 所属栏目:MsSql教程 来源:站长网
导读:导言 概述插入、更新和删除数据 里我们已经学习了如何使用GridView等控件来插入,更新删除数据。通过ObjectDataSource和其它数据控件仅仅只需要在智能标签里勾一下checkbox就完成了,不需要写任何代码。而DataList没有这些内置的功能。我们可以使用1.x 里

导言

  概述插入、更新和删除数据 里我们已经学习了如何使用GridView等控件来插入,更新删除数据。通过ObjectDataSource和其它数据控件仅仅只需要在智能标签里勾一下checkbox就完成了,不需要写任何代码。而DataList没有这些内置的功能。我们可以使用1.x 里的方法来实现这些功能。在本章我们将看到,DataList提供了一些事件和属性来完成我们的目的,为此我们需要写一些代码。

  本章我们首先学习如何创建一个支持编辑和删除数据的DataList。后面的教程里我们将学习一些高级的编辑和删除方法,包括验证,DAL和BLL的异常处理等。

  注意:和DataList一样,Repeater也不提供内置的这些功能。而且Repeater里没有DataList里提供的那些事件和属性。因此本章和后面的几章我们仅仅只讨论DataList。

第一步: 创建编辑和删除教程页

  首先创建本章和后面几章需要用到的页。添加一个名为EditDeleteDataList的文件夹。然后添加下面的页。确保每页都包含了Site.master。

Default.aspx
Basics.aspx
BatchUpdate.aspx
ErrorHandling.aspx
UIValidation.aspx
CustomizedUI.aspx
OptimisticConcurrency.aspx
ConfirmationOnDelete.aspx
UserLevelAccess.aspx

/uploads/allimg/c161121/14OI932D55F-463W7.png
图 1: 添加页

  和别的文件夹一样,Default.aspx列出教程章节。记得SectionLevelTutorialListing.ascx用户控件提供了这个功能。从解决方案里将它拖到我们的页里。

/uploads/allimg/c161121/14OI932G11Z-4JM8.png
图 2: 添加SectionLevelTutorialListing.ascx 用户控件

  最后将这些页添加到Web.sitemap里。在Master/Detail Reports with the DataList and Repeater<siteMapNode>之后添加下面的标记:

<siteMapNode
 title="Editing and Deleting with the DataList"
 description="Samples of Reports that Provide Editing and Deleting Capabilities"
 url="~/EditDeleteDataList/Default.aspx" >
 <siteMapNode
 title="Basics"
 description="Examines the basics of editing and deleting with the
   DataList control."
 url="~/EditDeleteDataList/Basics.aspx" />
 <siteMapNode
 title="Batch Update"
 description="Examines how to update multiple records at once in a
   fully-editable DataList."
 url="~/EditDeleteDataList/BatchUpdate.aspx" />
 <siteMapNode
 title="Error Handling"
 description="Learn how to gracefully handle exceptions raised during the
   data modification workflow."
 url="~/EditDeleteDataList/ErrorHandling.aspx" />
 <siteMapNode
 title="Adding Data Entry Validation"
 description="Help prevent data entry errors by providing validation."
 url="~/EditDeleteDataList/UIValidation.aspx" />
 <siteMapNode
 title="Customize the User Interface"
 description="Customize the editing user interfaces."
 url="~/EditDeleteDataList/CustomizedUI.aspx" />
 <siteMapNode
 title="Optimistic Concurrency"
 description="Learn how to help prevent simultaneous users from
   overwritting one another s changes."
 url="~/EditDeleteDataList/OptimisticConcurrency.aspx" />
 <siteMapNode
 title="Confirm On Delete"
 description="Prompt a user for confirmation when deleting a record."
 url="~/EditDeleteDataList/ConfirmationOnDelete.aspx" />
 <siteMapNode
 title="Limit Capabilities Based on User"
 description="Learn how to limit the data modification functionality
   based on the user s role or permissions."
 url="~/EditDeleteDataList/UserLevelAccess.aspx" />
</siteMapNode>

更新了Web.sitemap后,浏览一下。

/uploads/allimg/c161121/14OI932J0460-4U1N.png
图 3: 站点导航现在包含了编辑和删除DataList 教程

第二步: 探讨更新和删除数据所要用到的技术

  使用GridView来编辑和删除数据之所以很简单,是因为GridView和ObjectDataSource在底层非常一致。如研究插入、更新和删除的关联事件里所讨论的,当更新按钮被点击时,GridView自动将字段的值赋给ObjectDataSource的UpdateParameters集合,然后激发ObjectDataSource的Update()方法。我们现在需要确保将合适的值赋给ObjectDataSource的参数,然后调用Update()方法。DataList提供了以下的属性和事件来帮助我们完成这些:

  DataKeyField property — 更新或删除时,我们需要唯一确定DataList里的每个item。将这个属性设为显示的数据的主健。这样做会产生DataList的 DataKeys collection ,每个item都有一个指定的 DataKeyField .
 EditCommand event — 当CommandName属性设为“Edit”的Button, LinkButton, 或 ImageButton 被点时激发.
 CancelCommand event — 当CommandName属性设为“Cancel”的Button, LinkButton, 或 ImageButton 被点时激发. UpdateCommand event — 当CommandName属性设为“Update”的Button, LinkButton, 或 ImageButton 被点时激发.  DeleteCommand event — 当CommandName属性设为“Delete”的Button, LinkButton, 或 ImageButton 被点时激发.

  使用以上的属性和事件,我们有四种方法来更新和删除数据:

  使用ASP.NET 1.x 的技术— DataList先于ASP.NET 2.0 和ObjectDataSources 存在,可以直接通过编程来实现编辑和删除。这种方法需要我们在显示数据或者更新删除记录时,直接在BLL层将数据绑定到DataList。
使用一个单独的ObjectDataSource 来实现选择,更新和删除 — DataList没有GridView内置的编辑删除功能,并不意味着我们不能添加这些功能。我们象在GridView的例子里那样,使用 ObjectDataSource,但是在设置ObjectDataSource的参数并调用Update()方法时,需要为DataList的UpdateCommand事件创建一个  event handler。 Using an ObjectDataSource Control for Selecting, but Updating and Deleting Directly Against the BLL — 使用第二种方法时我们需要为UpdateCommand事件和参数赋值等写一些代码。其实我们可以用ObjectDataSource来实现selecting ,更新和删除直接调用BLL(象第一种方法)。我的意见是,直接调用BLL会使代码可读性更好。 使用多个ObjectDataSources —前面的三种方法都需要一些代码。如果你宁愿坚持使用尽可能多的声明语法的话,最后一种方法是使用多个ObjectDataSources 。第一个ObjectDataSource 从BLL获取数据,并绑定到 DataList. 为更新添加另一个 ObjectDataSource, 直接添加到 DataList的 EditItemTemplate.同样对删除也是如此。三个 ObjectDataSource通过 ControlParameters   声明语法直接将参数绑定到ObjectDataSource 的参数 (而不是在 DataList的 UpdateCommand event handler编程处理). 这种方法也需要一些编码 — 我们需要调用 ObjectDataSource内置的 Update() 或 Delete() — 但是比起其它三种方法,代码少的多。这种方法的劣势是多个  ObjectDataSources 使页面看起来混乱。

(编辑:源码网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

热点阅读