在ASP.NET 2.0中操作数据之六十四:GridView批量添加(5)

https://img.jbzj.com/file_images/article/201605/2016051811030963.png


图12:点击Process Product Shipment” 按钮后,将切换到添加界面.

第四步:添加产品

  剩下要做的事情是在“Add Products from Shipment”按钮的Click事件处理器里将产品添加到数据库.为此,我们可以创建一个ProductsDataTable,并为要插入的产品添加一个ProductsRow instance实例。一旦添加完ProductsRows后,我们就调用并把ProductsDataTable传递给ProductsBLL class类的UpdateWithTransaction方法.记得我们是在第61章《在事务里对数据库修改进行封装》里创建的UpdateWithTransaction方法,该方法将ProductsDataTable传递给ProductsTableAdapter的UpdateWithTransaction方法.于是启动一个ADO.NET事务,TableAdatper针对添加的每一个ProductsRow向数据库发出一个INSERT命令.如果所有的添加无误则提交事务,否则对事务回滚.

  对“Add Products from Shipment”按钮的Click处理器进行编码时,应该执行一些误差校验,因为我们没有在添加界面里使用RequiredFieldValidators控件,某个用户可能输入了产品的price而忽视里其name。由于产品的name是必须的,如果出现这种情况的话,我们需要提醒用户并中断inserts操作.完整的代码如下:

protected void AddProducts_Click(object sender, EventArgs e) { // Make sure that the UnitPrice CompareValidators report valid data... if (!Page.IsValid) return; // Add new ProductsRows to a ProductsDataTable... Northwind.ProductsDataTable products = new Northwind.ProductsDataTable(); for (int i = firstControlID; i <= lastControlID; i++) { // Read in the values for the product name and unit price string productName = ((TextBox)InsertingInterface.FindControl ("ProductName" + i.ToString())).Text.Trim(); string unitPrice = ((TextBox)InsertingInterface.FindControl ("UnitPrice" + i.ToString())).Text.Trim(); // Ensure that if unitPrice has a value, so does productName if (unitPrice.Length > 0 && productName.Length == 0) { // Display a warning and exit this event handler StatusLabel.Text = "If you provide a unit price you must also " + "include the name of the product."; StatusLabel.Visible = true; return; } // Only add the product if a product name value is provided if (productName.Length > 0) { // Add a new ProductsRow to the ProductsDataTable Northwind.ProductsRow newProduct = products.NewProductsRow(); // Assign the values from the web page newProduct.ProductName = productName; newProduct.SupplierID = Convert.ToInt32(Suppliers.SelectedValue); newProduct.CategoryID = Convert.ToInt32(Categories.SelectedValue); if (unitPrice.Length > 0) newProduct.UnitPrice = Convert.ToDecimal(unitPrice); // Add any "default" values newProduct.Discontinued = false; newProduct.UnitsOnOrder = 0; products.AddProductsRow(newProduct); } } // If we reach here, see if there were any products added if (products.Count > 0) { // Add the new products to the database using a transaction ProductsBLL productsAPI = new ProductsBLL(); productsAPI.UpdateWithTransaction(products); // Rebind the data to the grid so that the producst just added are displayed ProductsGrid.DataBind(); // Display a confirmation (don't use the Warning CSS class, though) StatusLabel.CssClass = string.Empty; StatusLabel.Text = string.Format( "{0} products from supplier {1} have been added and filed under " + "category {2}.", products.Count, Suppliers.SelectedItem.Text, Categories.SelectedItem.Text); StatusLabel.Visible = true; // Revert to the display interface ReturnToDisplayInterface(); } else { // No products supplied! StatusLabel.Text = "No products were added. Please enter the product " + "names and unit prices in the textboxes."; StatusLabel.Visible = true; } }

  该事件处理器开始时检查Page.IsValid属性返回的值是否为true。如果返回的为false,那就意味着至少有一个CompareValidators控件发现了无效的数据.此时,我们要么停止添加产品;要么将用户键入的unit price值向ProductsRow的UnitPrice属性赋值时,以抛出一个异常告终.

  接下来创建一个新的ProductsDataTable instance实例(也就是products),在一个for循环里遍历有关name和unit price的TextBox控件,并将其Text属性读取到局部变量productName 和 unitPrice里.如果用户输入了产品的unit price值而没有输入产品的name值,那么StatusLabel控件就会显示消息“If you provide a unit price you must also include the name of the product”并退出事件处理器.

  如果用户输入了产品name的话,就用ProductsDataTable的NewProductsRow方法创建一个新的ProductsRow instance实例.对实例的ProductName属性而言,是用相应的TextBox来赋值;而对SupplierID 和 CategoryID属性而言,是用添加界面顶部的相应的DropDownList控件的SelectedValue属性值来赋值的;如果用户输入里产品的价格,就对ProductsRow instance实例的UnitPrice属性进行赋值,如果用户没有输入价格那么就置空,向数据库添加数据时UnitPrice的值就为NULL值.最后,对Discontinued 和 UnitsOnOrder属性用硬编码的值“false”和“0”分别赋值.

  完成对ProductsRow instance实例的相关属性赋值后,将其添加到ProductsDataTable.

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

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