在ASP.NET 2.0中操作数据之三十六:在DataList里编辑和删除数据
现在点product的Update或Cancel button不会有任何反应。为了完成目标我们需要为DataList的UpdateCommand和CancelCommand创建event handler。首先创建CancelCommand event handler,它在product的Cancel button点击时执行,使DataList返回编辑之前的状态。使DataList以只读模式展示item,我们需要: 设置DataList的 EditItemIndex property 为一个不存在的DataListItem index -1是一个好的选择。(由于DataListItem index从0开始) 重新绑定数据到DataList。由于没有DataListItem ItemIndex和DataList的EditItemIndex关联,整个DataList会展现为只读模式。 这些可以通过以下代码完成: protected void DataList1_CancelCommand(object source, DataListCommandEventArgs e) { // Set the DataList's EditItemIndex property to -1 DataList1.EditItemIndex = -1; // Rebind the data to the DataList DataList1.DataBind(); } 现在点击Cancel button会返回到DataList编辑前的状态。 最后我们来完成UpdateCommand event handler,我们需要: 编程获取用户输入的product name和price,还有ProductID. 第一和第二步负责保存用户的更改。第三步返回到DataList编辑前的状态(和CancelCommand event handler一样)。 我们需要使用FindControl方法来获取product的name和price(当然包括ProductID)。当最初将ObjectDataSource绑定到DataList时,Visual Studio 将DataList的DataKeyField 属性赋为数据源的主键值(ProductID)。这个值可以通过DataList的DataKey集合来获取。花点时间验证一下DataKeyField 是否设置为ProductID。 下面的代码完成了上面的功能: protected void DataList1_UpdateCommand(object source, DataListCommandEventArgs e) { // Read in the ProductID from the DataKeys collection int productID = Convert.ToInt32(DataList1.DataKeys[e.Item.ItemIndex]); // Read in the product name and price values TextBox productName = (TextBox)e.Item.FindControl("ProductName"); TextBox unitPrice = (TextBox)e.Item.FindControl("UnitPrice"); string productNameValue = null; if (productName.Text.Trim().Length > 0) productNameValue = productName.Text.Trim(); decimal? unitPriceValue = null; if (unitPrice.Text.Trim().Length > 0) unitPriceValue = Decimal.Parse(unitPrice.Text.Trim(), System.Globalization.NumberStyles.Currency); // Call the ProductsBLL's UpdateProduct method... ProductsBLL productsAPI = new ProductsBLL(); productsAPI.UpdateProduct(productNameValue, unitPriceValue, productID); // Revert the DataList back to its pre-editing state DataList1.EditItemIndex = -1; DataList1.DataBind(); } 首先从DataKeys集合里读出product的ProductID。然后将两个TextBox的Text属性存起来。我们用Decimal.Parse() 方法去读UnitPrice TextBox的值,以便在这个值有货币符号时可以正确的转换。 注意:只有在TextBox的Text指定了值的情况下,productNameValue和unitPriceValue变量才会被赋值。否则,会在更新数据时使用一个NULL值。也就是说我们的代码会将空字符串转换为NULL值,而在GridView,DetailsView和FormView控件的编辑界面里NULL是缺省值。获取值后,调用ProductsBLL类的UpdateProduct方法,将product的name,price和ProductID传进去。使用和CancelCommand event handler里同样的逻辑返回到DataList编辑前状态。完成了EditCommand,CancelCommand和UpdateCommand event handler后,用户可以编辑product的name和price了。见图14。
第七步: 增加删除功能 增加删除功能的步骤和增加编辑功能差不多。简单来说我们需要在ItemTemplate里添加一个Delete button,当点击时: 从DataKeys 集合里读取关联的proudct的ProductID . 当点击一个CommandName为“Edit”, “Update”, 或“Cancel”的Button时,会激发DataList的ItemCommand事件和另外一个事件(比如,使用“Edit”时EditCommand 事件会被激发)。同样的,CommandName为“Delete”时会激发DeleteCommand 事件(和ItemCommand一起)。 在Edit button后面增加一个Delete button ,将CommandName属性设为“Delete”. 完成后声明代码如下: <ItemTemplate> <h5> <asp:Label runat="server" ID="ProductNameLabel" Text='<%# Eval("ProductName") %>' /> </h5> Price: <asp:Label runat="server" ID="Label1" Text='<%# Eval("UnitPrice", "{0:C}") %>' /> <br /> <asp:Button runat="server" id="EditProduct" CommandName="Edit" Text="Edit" /> <asp:Button runat="server" id="DeleteProduct" CommandName="Delete" Text="Delete" /> <br /> <br /> </ItemTemplate> (编辑:PHP编程网 - 黄冈站长网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |