在ASP.NET 2.0中操作数据之三十八:处理BLL和DAL的异

  在DataList里编辑和删除数据概述里,我们创建了一个提供简单编辑和删除功能的DataList。虽然功能上已经完整了,但是对用户来说是不友好的。因为所有在编辑和删除过程中产生的异常都是未处理的。比如,遗漏了输入product的name,或者编辑product时在price里输入“Very affordable!”,都会抛出异常。而由于在代码里未捕捉这些异常,页面会显示ASP.NET运行时的详细错误信息。

  如我们在在ASP.NET页面中处理BLL/DAL层的异常里看到的,如果BLL或DAL里发生异常,详细的信息会返回到ObjectDataSource,然后再到GridView。我们已经学习了如何优雅的处理这些异常:为ObjectDataSource或GridView创建Updated或RowUpdated事件处理,检查异常,然后指明异常被处理。

  然而在使用DataList时,我们并没有通过ObjectDataSource来更新和删除数据。我们是直接通过BLL来实现的。为了检测到 BLL或DAL的异常,我们需要在ASP.NET页里写异常处理代码。本章我们将学习在使用DataList编辑时如何巧妙的处理异常。

  注意:在DataList里编辑和删除数据概述里,我们讨论了几种不同的编辑和删除数据的方法,其中一些会涉及到使用ObjectDataSource来编辑和删除。如果你用这些技术的话,你可以直接通过ObjectDataSource的Updated或Deleted 事件处理中处理这些异常。

第一步: 创建一个可编辑的DataList

  首先创建一个可编辑的DataList。打开EditDeleteDataList文件夹下的ErrorHandling.aspx页,添加一个ID为Products的DataList和一个名为ProductsDataSource的ObjectDataSouce。在SELECT标签下选择ProductsBLL类的GetProducts()方法。在INSERT,UPDATE和DELETE标签里选择None.

/uploads/allimg/200612/1J21362C_0.png


图 1: 配置ObjectDataSource

  完成ObjectDataSouce后,Visual Studio会自动创建一个ItemTemplate。用显示每个product的name和price并包含一个Edit button的ItemTemplate代替它,然后创建一个用TextBox显示name和price,并包含一个Update button和Cancel button的EditItemTemplate。最后将DataList的RepeatColumns属性设为2。

  做完这些后,你的声明代码应该和下面的差不多。检查并确保Edit,Cancel和Update button的CommandName属性,分别被设为“Edit”, “Cancel”, 和“Update”。

<asp:DataList runat="server" DataKeyField="ProductID" DataSourceID="ProductsDataSource" RepeatColumns="2"> <ItemTemplate> <h5> <asp:Label runat="server" Text='<%# Eval("ProductName") %>' /> </h5> Price: <asp:Label runat="server" Text='<%# Eval("UnitPrice", "{0:C}") %>' /> <br /> <asp:Button runat="server" CommandName="Edit" Text="Edit" /> <br /> <br /> </ItemTemplate> <EditItemTemplate> Product name: <asp:TextBox runat="server" Text='<%# Eval("ProductName") %>' /> <br /> Price: <asp:TextBox runat="server" Text='<%# Eval("UnitPrice", "{0:C}") %>' /> <br /> <br /> <asp:Button runat="server" CommandName="Update" Text="Update" /> <asp:Button runat="server" CommandName="Cancel" Text="Cancel" /> </EditItemTemplate> </asp:DataList> <asp:ObjectDataSource runat="server" SelectMethod="GetProducts" TypeName="ProductsBLL" OldValuesParameterFormatString="original_{0}"> </asp:ObjectDataSource>          

  注意:本章里DataList的view state必须开启。浏览一下页面,见图2。

/uploads/allimg/200612/1J210X56_0.png


图 2: 每个Product 都包含一个Edit Button

  现在Edit button只是引起一个postback —还不能将product变成可编辑的。为了实现编辑功能,我们需要为EditCommand,CancelCommand和UpdateCommand创建事件处理。EditCommand和CancelCommand事件仅仅只需要更新DataList的EditItemIndex属性,并重新绑定数据到DataList。

protected void Products_EditCommand(object source, DataListCommandEventArgs e) { // Set the DataList's EditItemIndex property to the // index of the DataListItem that was clicked Products.EditItemIndex = e.Item.ItemIndex; // Rebind the data to the DataList Products.DataBind(); } protected void Products_CancelCommand(object source, DataListCommandEventArgs e) { // Set the DataList's EditItemIndex property to -1 Products.EditItemIndex = -1; // Rebind the data to the DataList Products.DataBind(); }        

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wjwypx.html