在ASP.NET 2.0中操作数据之五十一:从GridView的页脚(4)

  将每个validation控件的ForeColor属性清空,以便用在FooterStyle CSS类定义的白色背景色取代默认的红色;同时将ErrorMessage设置为详细的错误提示;将Text属性设置为星号。在每个FooterTemplates里,只要包含有validation控件,将其Wrap属性设置为false。最后,在GridView控件下面添加一个ValidationSummary 控件,设ShowMessageBox属性为true;ShowSummary属性为false。

  当添加一个新产品时,我们需要给出CategoryID和SupplierID值。页面上的2个DropDownList控件显示的是CategoryName 和SupplierName,但传递的是我们需要的
CategoryID和SupplierID值。为什么不直接显示CategoryID和SupplierID值呢?因为最终用户对CategoryName 和SupplierName更感兴趣。既然现在可以在显示CategoryName 和SupplierName的插入界面获取对应的CategoryID和SupplierID值,我们将CategoryID 和SupplierID 2个TemplateFields从GridView移除。

  同样,当添加新产品时我们不需要ProductID,那么我们也可以删除ProductID TemplateField,不过,在这里我们保留它。除了TextBoxes,DropDownLists、
CheckBoxes以及validation控件外,我们还需要在插入界面添加一个“Add”按钮。当点击该按钮时,将新记录添加到数据库。在第4步,我们将在ProductID TemplateField的FooterTemplate模板添加一个“Add”按钮。

  按你喜欢的方式改进外观。比如,将UnitPrice值格式化为货币形式;将UnitsInStock, UnitsOnOrder和ReorderLevel三列放在右边;修改TemplateFields的HeaderText属性等。

  在FooterTemplates里完成插入界面的修改后,移除SupplierID 和 CategoryID TemplateFields,最终,你的GridView控件的声明代码看起来应该和下面的差不多:

<asp:GridView runat="server" AutoGenerateColumns="False" DataKeyNames="ProductID" DataSourceID="ProductsDataSource" AllowPaging="True" EnableViewState="False" ShowFooter="True"> <Columns> <asp:TemplateField HeaderText="ProductID" InsertVisible="False" SortExpression="ProductID"> <ItemTemplate> <asp:Label runat="server" Text='<%# Bind("ProductID") %>'></asp:Label> </ItemTemplate> <ItemStyle HorizontalAlign="Center" /> </asp:TemplateField> <asp:TemplateField HeaderText="Product" SortExpression="ProductName"> <ItemTemplate> <asp:Label runat="server" Text='<%# Bind("ProductName") %>'></asp:Label> </ItemTemplate> <FooterTemplate> <asp:TextBox runat="server"></asp:TextBox> <asp:RequiredFieldValidator runat="server" ControlToValidate="NewProductName" Display="Dynamic" ForeColor="" ErrorMessage="You must enter a name for the new product."> * </asp:RequiredFieldValidator> </FooterTemplate> <FooterStyle Wrap="False" /> </asp:TemplateField> <asp:TemplateField HeaderText="Category" SortExpression="CategoryName"> <ItemTemplate> <asp:Label runat="server" Text='<%# Bind("CategoryName") %>'></asp:Label> </ItemTemplate> <FooterTemplate> <asp:DropDownList runat="server" DataSourceID="CategoriesDataSource" DataTextField="CategoryName" DataValueField="CategoryID"> </asp:DropDownList> <asp:ObjectDataSource runat="server" OldValuesParameterFormatString="original_{0}" SelectMethod="GetCategories" TypeName="CategoriesBLL"> </asp:ObjectDataSource> </FooterTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Supplier" SortExpression="SupplierName"> <ItemTemplate> <asp:Label runat="server" Text='<%# Bind("SupplierName") %>'></asp:Label> </ItemTemplate> <FooterTemplate> <asp:DropDownList runat="server" DataSourceID="SuppliersDataSource" DataTextField="CompanyName" DataValueField="SupplierID"> </asp:DropDownList><asp:ObjectDataSource runat="server" OldValuesParameterFormatString="original_{0}" SelectMethod="GetSuppliers" TypeName="SuppliersBLL"> </asp:ObjectDataSource> </FooterTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Qty/Unit" SortExpression="QuantityPerUnit"> <ItemTemplate> <asp:Label runat="server" Text='<%# Bind("QuantityPerUnit") %>'></asp:Label> </ItemTemplate> <FooterTemplate> <asp:TextBox runat="server"></asp:TextBox> </FooterTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Price" SortExpression="UnitPrice"> <ItemTemplate> <asp:Label runat="server" Text='<%# Bind("UnitPrice", "{0:c}") %>'></asp:Label> </ItemTemplate> <FooterTemplate> $<asp:TextBox runat="server" Columns="8" /> <asp:CompareValidator runat="server" ControlToValidate="NewUnitPrice" ErrorMessage="You must enter a valid currency value greater than or equal to 0.00. Do not include the currency symbol." ForeColor="" Operator="GreaterThanEqual" Type="Currency" ValueToCompare="0" Display="Dynamic"> * </asp:CompareValidator> </FooterTemplate> <ItemStyle HorizontalAlign="Right" /> <FooterStyle Wrap="False" /> </asp:TemplateField> <asp:TemplateField HeaderText="Units In Stock" SortExpression="Units In Stock"> <ItemTemplate> <asp:Label runat="server" Text='<%# Bind("UnitsInStock") %>'></asp:Label> </ItemTemplate> <FooterTemplate> <asp:TextBox runat="server" Columns="5" /> <asp:CompareValidator runat="server" ControlToValidate="NewUnitsInStock" Display="Dynamic" ErrorMessage="You must enter a valid numeric value for units in stock that's greater than or equal to zero." ForeColor="" Operator="GreaterThanEqual" Type="Integer" ValueToCompare="0">*</asp:CompareValidator> </FooterTemplate> <ItemStyle HorizontalAlign="Right" /> <FooterStyle Wrap="False" /> </asp:TemplateField> <asp:TemplateField HeaderText="Units On Order" SortExpression="UnitsOnOrder"> <ItemTemplate> <asp:Label runat="server" Text='<%# Bind("UnitsOnOrder") %>'></asp:Label> </ItemTemplate> <FooterTemplate> <asp:TextBox runat="server" Columns="5" /> <asp:CompareValidator runat="server" ControlToValidate="NewUnitsOnOrder" Display="Dynamic" ErrorMessage="You must enter a valid numeric value for units on order that's greater than or equal to zero." ForeColor="" Operator="GreaterThanEqual" Type="Integer" ValueToCompare="0">*</asp:CompareValidator> </FooterTemplate> <ItemStyle HorizontalAlign="Right" /> <FooterStyle Wrap="False" /> </asp:TemplateField> <asp:TemplateField HeaderText="Reorder Level" SortExpression="ReorderLevel"> <ItemTemplate> <asp:Label runat="server" Text='<%# Bind("ReorderLevel") %>'></asp:Label> </ItemTemplate> <FooterTemplate> <asp:TextBox runat="server" Columns="5" /> <asp:CompareValidator runat="server" ControlToValidate="NewReorderLevel" Display="Dynamic" ErrorMessage="You must enter a valid numeric value for reorder level that's greater than or equal to zero." ForeColor="" Operator="GreaterThanEqual" Type="Integer" ValueToCompare="0">*</asp:CompareValidator> </FooterTemplate> <ItemStyle HorizontalAlign="Right" /> <FooterStyle Wrap="False" /> </asp:TemplateField> <asp:TemplateField HeaderText="Discontinued" SortExpression="Discontinued"> <ItemTemplate> <asp:CheckBox runat="server" Checked='<%# Bind("Discontinued") %>' Enabled="false" /> </ItemTemplate> <FooterTemplate> <asp:CheckBox runat="server" /> </FooterTemplate> <ItemStyle HorizontalAlign="Center" /> <FooterStyle HorizontalAlign="Center" /> </asp:TemplateField> </Columns> </asp:GridView>

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

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