51从GridView的页脚插入新记录.docx
- 文档编号:7391232
- 上传时间:2023-01-23
- 格式:DOCX
- 页数:28
- 大小:645.70KB
51从GridView的页脚插入新记录.docx
《51从GridView的页脚插入新记录.docx》由会员分享,可在线阅读,更多相关《51从GridView的页脚插入新记录.docx(28页珍藏版)》请在冰豆网上搜索。
51从GridView的页脚插入新记录
五十一:
从GridView的页脚插入新记录
导言:
正如教程《概述插入、更新和删除数据》里探讨过的一样,GridView,DetailsView和FormViewWeb控件都有内置的修改数据的功能。
当声明绑定到数据源控件时,可以快速而方便地修改数据——甚至不用写一行代码。
不幸的是,只有DetailsView和FormView控件提供了内置的插入、编辑、删除功能,而GridView控件只支持编辑、删除功能。
不过,稍许努力,我们就能使GridView控件包含一个插入界面。
为了给GridView添加插入功能,我们要决定如何添加新记录:
创建插入界面,编码插入数据。
在本教程,我们将为GridView的页脚行(footerrow)添加插入界面(见图1)。
其中每一列包含相应的用户界面元件(比如在TextBox里输入产品名称,在DropDownLis里选择供应商等等),同时我们需要一个"Add"按钮,当点击时,发生页面回传,将新记录添加到表Products里。
图1:
页脚行提供了一个添加新记录的界面
第一步:
在GridView控件里展示产品信息
首先添加一个展示产品的GridView控件。
打开EnhancedGridView文件夹里的InsertThroughFooter.aspx页面,在上面添加一个GridView控件,设其ID为Products,然后,在其智能标签里绑定到一个名为ProductsDataSource的ObjectDataSource。
图2:
创建一个名为ProductsDataSource的新ObjectDataSource
设置该ObjectDataSource调用ProductsBLL类的GetProducts()方法获取产品信息。
在本教程里,我们只关注于添加插入功能,与编辑和删除无关。
所以,确保在“插入”选项卡里选AddProduct()方法。
而在“编辑”和“删除”里选“(None)”。
图3:
将ObjectDataSource的Insert()方法设置为AddProduct()
图4:
在UPDATE和DELETE选项里选“(None)”
完成设置后,VisualStudio会自动添加相关列。
现在,我们暂时不管这些列,在教程后续部分,我们将移除一些列,因为在添加新记录时我们不需指定这些列的值。
因为数据库中大概有80个产品,所以我们最好还是启用分页功能,以便使插入界面更直观、更易操作。
回到页面,在GridView的智能标签里启用分页。
现在,GridView和ObjectDataSource的声明代码看起来和下面的差不多:
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
GridViewID="Products"runat="server"AutoGenerateColumns="False" DataKeyNames="ProductID"DataSourceID="ProductsDataSource" AllowPaging="True"EnableViewState="False"> BoundFieldDataField="ProductID"HeaderText="ProductID" InsertVisible="False"ReadOnly="True" SortExpression="ProductID"/> BoundFieldDataField="ProductName"HeaderText="ProductName" SortExpression="ProductName"/> BoundFieldDataField="SupplierID"HeaderText="SupplierID" SortExpression="SupplierID"/> BoundFieldDataField="CategoryID"HeaderText="CategoryID" SortExpression="CategoryID"/> BoundFieldDataField="QuantityPerUnit"HeaderText="QuantityPerUnit" SortExpression="QuantityPerUnit"/> BoundFieldDataField="UnitPrice"HeaderText="UnitPrice" SortExpression="UnitPrice"/> BoundFieldDataField="UnitsInStock"HeaderText="UnitsInStock" SortExpression="UnitsInStock"/> BoundFieldDataField="UnitsOnOrder"HeaderText="UnitsOnOrder" SortExpression="UnitsOnOrder"/> BoundFieldDataField="ReorderLevel"HeaderText="ReorderLevel" SortExpression="ReorderLevel"/> CheckBoxFieldDataField="Discontinued"HeaderText="Discontinued" SortExpression="Discontinued"/> BoundFieldDataField="CategoryName"HeaderText="CategoryName" ReadOnly="True"SortExpression="CategoryName"/> BoundFieldDataField="SupplierName"HeaderText="SupplierName" ReadOnly="True"SortExpression="SupplierName"/> GridView> ObjectDataSourceID="ProductsDataSource"runat="server" InsertMethod="AddProduct"OldValuesParameterFormatString="original_{0}" SelectMethod="GetProducts"TypeName="ProductsBLL"> ParameterName="productName"Type="String"/> ParameterName="supplierID"Type="Int32"/> ParameterName="categoryID"Type="Int32"/> ParameterName="quantityPerUnit"Type="String"/> ParameterName="unitPrice"Type="Decimal"/> ParameterName="unitsInStock"Type="Int16"/> ParameterName="unitsOnOrder"Type="Int16"/> ParameterName="reorderLevel"Type="Int16"/> ParameterName="discontinued"Type="Boolean"/> ObjectDataSource> 图5: 在一个启用了分页功能的GridView里,显示产品的所有数据项 第2步: 添加一个页脚行 GridView控件包含页眉行、数据行和页脚行。 GridView控件ShowHeader和ShowFooter属性决定了是否显示页眉行和页脚行。 如果要显示页脚行,我们需要将ShowFooter属性设置为true。 如图6所示: 图6: 设ShowFooter属性为True,添加页脚行 我们注意到页脚行的背景色是深红色,这是由于我们在教程《使用ObjectDataSource展现数据》里创建了一个名为DataWebControls的主题,并将其应用为所有的页面底色。 特别的,皮肤文件GridView.skin设置FooterStyle属性使用FooterStyleCSS,其代码如下: ? 1 2 3 4 5 6 .FooterStyle { background-color: #a33; color: White; text-align: right; } 注意: 在以前的教程我们提到过使用GridView的页脚行。 如果不清楚的话,请查阅教程第15章《在GridView的页脚中显示统计信息》 设置ShowFooter属性为true后,在浏览器里观看效果。 当前的页脚行并不包含任何的文字或Web控件。 在第3步,我们将修改其包含相应的插入界面。 图7: 页脚行显示为空白 第3步: 自定义页脚行 回顾教程《在GridView控件中使用TemplateField》,在那篇教程我们探讨了如何对GridView的某一列使用TemplateFields(而不是BoundFields或CheckBoxFields),从而实现自定义显示样式;而在教程《定制数据修改界面》里我们看到如何在GridView里使用TemplateFields定制编辑界面。 一个TemplateField是由诸如ItemTemplate、EditItemTemplate等模板构成的。 比如,ItemTemplate模板显示的数据行为只读状态;而EditItemTemplate模板定制了一个编辑行界面。 除了ItemTemplate、EditItemTemplate等模板外,TemplateField也包含一个名为FooterTemplate的模板,它为容器指定页脚行。 所以我们可以在FooterTemplate模板里添加插入界面要用到的Web控件。 让我们开始吧,首先,我们将GridView控件里的所有列转换成TemplateFields。 在GridView控件的智能标签里点击“编辑列”,在左边选中每个域,再点击“ConvertthisfieldintoaTemplateField”。 图8: 将每个域转换为一个TemplateField 点击“ConvertthisfieldintoaTemplateField”的话,将当前类型的域转换成相应的TemplateField。 比如,每个BoundField将转换成这样的TemplateField,它的ItemTemplate包含一个Label控件来显示相应的数据域;它的EditItemTemplate使用一个TextBox控件来显示相应的数据域。 例如,在这里,名为ProductName的BoundField将被转换为如下所示的TemplateField: ? 1 2 3 4 5 6 7 8 9 10 TemplateFieldHeaderText="ProductName"SortExpression="ProductName"> TextBoxID="TextBox1"runat="server" Text='<%#Bind("ProductName")%>'> TextBox> LabelID="Label2"runat="server" Text='<%#Bind("ProductName")%>'> Label> TemplateField> 同样的,名为Discontinued的CheckBoxField转换为TemplateField后,其ItemTemplate和EditItemTemplate模板都将包含一个CheckBoxWeb控件(只是ItemTemplate模板里的CheckBox不可用);而处于“只读”状态的ProductIDBoundField转换成TemplateField后,其ItemTemplate和EditItemTemplate模板都包含一个Label控件。 简而言之,将GridView里的某一列转换为一个TemplateField,是定制自定义模板的一种又快又容易的方法,且不会丧失该列应有的功能。 由于我们不需要GridView支持编辑功能,将每个TemplateField的EditItemTemplate模板删除,只留下ItemTemplate模板。 完成后,GridView的代码看起来应和下面的差不多: ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 GridViewID="Products"runat="server"AutoGenerateColumns="False" DataKeyNames="ProductID"DataSourceID="ProductsDataSource" AllowPaging="True"EnableViewState="False"ShowFooter="True"> TemplateFieldHeaderText="ProductID"InsertVisible="False" SortExpression="ProductID"> LabelID="Label1"runat="server" Text='<%#Bind("ProductID")%>'> Label> TemplateField> TemplateFieldHeaderText="ProductName"SortExpression="ProductName"> LabelID="Label2"runat="server" Text='<%#Bind("ProductName")%>'> Label> TemplateField> TemplateFieldHeaderText="SupplierID"SortExpression="SupplierID"> LabelID="Label3"runat="server" Text='<%#Bind("SupplierID")%>'> Label> TemplateField> TemplateFieldHeaderText="CategoryID"SortExpression="CategoryID"> LabelID="Label4"runat="server" Text='<%#Bind("CategoryID")%>'> Label> TemplateField> TemplateFieldHeaderText="QuantityPerUnit" SortExpression="QuantityPerUnit"> LabelID="Label5"runat="server" Text='<%#Bind("QuantityPerUnit")%>'> Label> TemplateField> TemplateFieldHeaderText="UnitPrice"SortExpression="UnitPrice"> LabelID="Label6"runat="server" Text='<%#Bind("UnitPrice")%>'> Label> TemplateField> TemplateFieldHeaderText="UnitsInStock" SortExpression="UnitsInStock"> LabelID="Label7"runat="server" Text='<%#Bind("UnitsInStock")%>'> Label> TemplateField> TemplateFieldHeaderText="UnitsOnOrder" SortExpression="UnitsOnOrder"> LabelID="Label8"runat="server" Text='<%#Bind("UnitsOnOrder")%>'> Label> TemplateField> TemplateFieldHeaderText="ReorderLevel" SortExpression="ReorderLevel"> LabelID="Label9"runat="server" Text='<%#Bind("ReorderLevel")%>'> Label> TemplateField> TemplateFieldHeaderText="Discontinued" SortExpression="Discontinued"> CheckBoxID="CheckBox1"runat="server" Checked='<%#Bind("Discontinued")%>'Enabled="false"/> TemplateField> TemplateFieldHeaderText="CategoryName" SortExpression="CategoryName"> LabelID="Label10"runat="server" Text='<%#Bind("CategoryName")%>'> Label> TemplateField> TemplateFieldHeaderText="SupplierName" SortExpression="SupplierName"> LabelID="Label11"runat="server" Text='<%#Bind("SupplierName")%>'> Label> TemplateField> GridView> 现在,每个GridView列都已经转换成一个TemplateField,我们在其FooterTemplate里添加适当的插入界面。 然而,有些列没有插入界面(比如ProductID),其它列的TemplateField模板将包含Web控件,供用户输入产品信息。 在GridView的智能标签里点击“EditTemplates”,从下拉列表里选择某列的FooterTemplate模板,从工具箱里拖一个适当的控件到页面上。 图9: 在每列的FooterTemplate里添加适当的插入界面。 下面列出了GridView的所有列,并指定每列添加哪些插入界面: ProductID–无 ProductName–添加一个TextBox,ID为NewProductName;再添加一个 RequiredFieldValidator控件,防止用户未输入产品名。 SupplierID–无 CategoryID–无 QuantityPerUnit–添加一个TextBox,ID为NewQuantityPerUnit UnitPrice–添加一个TextBox,ID为NewUnitPrice,再添加一个CompareValidator控件,确保用户输入的是货币值,且>=0 UnitsInStock–添加一个TextBox,ID为NewUnitsInStock,再添加一个CompareValidator控件,确保用户输入的是整数值,且>=0 UnitsOnOrder–添加一个TextBox,ID为NewUnitsOnOrder,再添加一个Compare
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 51 GridView 插入 记录