在ASP.NET 2.0中操作数据之六十二:GridView批量更新(4)

  然后,将Discontinued列里的checkbox控件居中(而不是居左),在GridView的智能标签里点“编辑列”,选取左边方框里的Discontinued,再在右边方框里的ItemStyle里将HorizontalAlign属性设置为Center,如图15所示:

/uploads/allimg/200612/1I1221325_0.gif


图15:将Discontinued列里的CheckBox居左

  接下来在页面上添加一个ValidationSummar控件,将其ShowMessageBox属性设置为true;ShowSummary属性设置为false. 同时再添加一个Button Web控件,用来更新用户所做的更该。特别的,添加2个,一个在GridView控件上面,一个在下面,将它们的Text属性设置为“Update Products”.由于我们已经在TemplateFields模板定义了编辑界面,那么EditItemTemplates模板就显得多余了,将其删除.

完成上述修改后,你的页面声明代码看起来应该和下面的差不多:

<p> <asp:Button runat="server" Text="Update Products" /> </p> <p> <asp:GridView runat="server" AutoGenerateColumns="False" DataKeyNames="ProductID" DataSourceID="ProductsDataSource" AllowPaging="True" AllowSorting="True"> <Columns> <asp:TemplateField HeaderText="Product" SortExpression="ProductName"> <ItemTemplate> <asp:TextBox runat="server" Text='<%# Bind("ProductName") %>'></asp:TextBox> <asp:RequiredFieldValidator ControlToValidate="ProductName" ErrorMessage="You must provide the product's name." runat="server">*</asp:RequiredFieldValidator> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Category" SortExpression="CategoryName"> <ItemTemplate> <asp:DropDownList runat="server" AppendDataBoundItems="True" DataSourceID="CategoriesDataSource" DataTextField="CategoryName" DataValueField="CategoryID" SelectedValue='<%# Bind("CategoryID") %>'> <asp:ListItem>-- Select One --</asp:ListItem> </asp:DropDownList> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Price" SortExpression="UnitPrice"> <ItemTemplate> $<asp:TextBox runat="server" Columns="8" Text='<%# Bind("UnitPrice", "{0:N}") %>'></asp:TextBox> <asp:CompareValidator runat="server" ControlToValidate="UnitPrice" ErrorMessage="You must enter a valid currency value. Please omit any currency symbols." Operator="GreaterThanEqual" Type="Currency" ValueToCompare="0">*</asp:CompareValidator> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Discontinued" SortExpression="Discontinued"> <ItemTemplate> <asp:CheckBox runat="server" Checked='<%# Bind("Discontinued") %>' /> </ItemTemplate> <ItemStyle HorizontalAlign="Center" /> </asp:TemplateField> </Columns> </asp:GridView> </p> <p> <asp:Button runat="server" Text="Update Products" /> <asp:ObjectDataSource runat="server" OldValuesParameterFormatString="original_{0}" SelectMethod="GetProducts" TypeName="ProductsBLL"> </asp:ObjectDataSource> <asp:ObjectDataSource runat="server" OldValuesParameterFormatString="original_{0}" SelectMethod="GetCategories" TypeName="CategoriesBLL"> </asp:ObjectDataSource> <asp:ValidationSummary runat="server" ShowMessageBox="True" ShowSummary="False" /> </p>

当添加Button Web控件并对相关格式进行修改后,页面如下图所示:

/uploads/allimg/200612/1I1231159_0.gif


图16:页面现在包含了2个“Update Products”按钮

第五步:更新产品

  当用户登录该页面进行修改时并点击“Update Products”按钮时,我们需要将用户输入的值保存为一个ProductsDataTable instance实例;再将该实例传递给一个BLL method方法,进而将该实例传递给DAL层的UpdateWithTransaction  method方法。该方法是在前面的文章里创建的,确保对批处理进行原子操作.

在BatchUpdate.aspx.cs文件里创建一个名为BatchUpdate的方法,代码如下:

private void BatchUpdate() { // Enumerate the GridView's Rows collection and create a ProductRow ProductsBLL productsAPI = new ProductsBLL(); Northwind.ProductsDataTable products = productsAPI.GetProducts(); foreach (GridViewRow gvRow in ProductsGrid.Rows) { // Find the ProductsRow instance in products that maps to gvRow int productID = Convert.ToInt32(ProductsGrid.DataKeys[gvRow.RowIndex].Value); Northwind.ProductsRow product = products.FindByProductID(productID); if (product != null) { // Programmatically access the form field elements in the // current GridViewRow TextBox productName = (TextBox)gvRow.FindControl("ProductName"); DropDownList categories = (DropDownList)gvRow.FindControl("Categories"); TextBox unitPrice = (TextBox)gvRow.FindControl("UnitPrice"); CheckBox discontinued = (CheckBox)gvRow.FindControl("Discontinued"); // Assign the user-entered values to the current ProductRow product.ProductName = productName.Text.Trim(); if (categories.SelectedIndex == 0) product.SetCategoryIDNull(); else product.CategoryID = Convert.ToInt32(categories.SelectedValue); if (unitPrice.Text.Trim().Length == 0) product.SetUnitPriceNull(); else product.UnitPrice = Convert.ToDecimal(unitPrice.Text); product.Discontinued = discontinued.Checked; } } // Now have the BLL update the products data using a transaction productsAPI.UpdateWithTransaction(products); }

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

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