向编辑和插入界面添加验证控件 (C#)
在本教程中,我们将了解如何轻松地将验证控件添加到数据 Web 控件的 EditItemTemplate 和 InsertItemTemplate,以提供更万无一失的用户界面。
简介
我们在过去三个教程中探索的示例中的 GridView 和 DetailsView 控件均由 BoundFields 和 CheckBoxFields 组成, (通过智能标记) 将 GridView 或 DetailsView 绑定到数据源控件时 Visual Studio 自动添加的字段类型。 在 GridView 或 DetailsView 中编辑行时,那些不是只读的 BoundField 将转换为文本框,最终用户可以从中修改现有数据。 同样,在将新记录插入 DetailsView 控件时,属性 InsertVisible
设置为 true
(默认) 的 BoundField 将呈现为空文本框,用户可以在其中提供新记录的字段值。 同样,在标准只读接口中禁用的 CheckBoxFields 将转换为编辑和插入接口中的启用复选框。
虽然 BoundField 和 CheckBoxField 的默认编辑和插入接口可能很有用,但该接口缺少任何类型的验证。 如果用户出现数据输入错误(例如省略 ProductName
字段或为 UnitsInStock
(输入无效值,例如 -50) 将从应用程序体系结构的深度引发异常。 虽然此异常可以正常处理,如 上一教程所示,但理想情况下,编辑或插入用户界面将包含验证控件,以防止用户首先输入此类无效数据。
为了提供自定义的编辑或插入界面,我们需要将 BoundField 或 CheckBoxField 替换为 TemplateField。 TemplateFields(在 GridView 控件中使用 TemplateFields 和 DetailsView 控件 教程 中的使用 TemplateFields )中讨论的主题,可以包含多个模板,这些模板定义不同行状态的单独接口。 在 DetailsView 或 GridView 控件中呈现只读字段或行时,TemplateField ItemTemplate
的 用于 ,而 EditItemTemplate
和 InsertItemTemplate
分别指示用于编辑和插入模式的接口。
在本教程中,我们将了解如何轻松地将验证控件添加到 TemplateField 的 ,并提供InsertItemTemplate
更万无一失的EditItemTemplate
用户界面。 具体而言,本教程采用 检查与插入、更新和删除相关的事件 教程中创建的示例,并扩充了编辑和插入接口以包含适当的验证。
步骤 1:复制检查与插入、更新和删除关联的事件的示例
在 检查与插入、更新和删除相关的事件 教程中,我们创建了一个页面,其中列出了可编辑的 GridView 中产品的名称和价格。 此外,该页还包含一个 DetailsView,其 DefaultMode
属性设置为 Insert
,因此始终以插入模式呈现。 在此 DetailsView 中,用户可以输入新产品的名称和价格,单击“插入”,并将其添加到系统 (请参阅图 1) 。
图 1:前面的示例允许用户添加新产品和编辑现有产品 (单击以查看全尺寸图像)
本教程的目标是增强 DetailsView 和 GridView 以提供验证控件。 具体而言,我们的验证逻辑将:
- 要求在插入或编辑产品时提供名称
- 要求在插入记录时提供价格;编辑记录时,我们仍需要价格,但会使用前面教程中已存在的 GridView
RowUpdating
事件处理程序中的编程逻辑 - 确保为价格输入的值是有效的货币格式
在了解如何扩充前面的示例以包含验证之前,我们首先需要将示例从 DataModificationEvents.aspx
本教程 UIValidation.aspx
的页面复制到页面。 为此,我们需要复制 DataModificationEvents.aspx
页面的声明性标记及其源代码。 通过执行以下步骤,首先复制声明性标记:
- 在
DataModificationEvents.aspx
Visual Studio 中打开页面 - 转到页面的声明性标记 (单击页面底部的“源”按钮)
- 将 和
</asp:Content>
标记中的<asp:Content>
文本 (行 3 到 44) 复制,如图 2 所示。
图 2:复制控件中的 <asp:Content>
文本 (单击以查看全尺寸图像)
- 打开
UIValidation.aspx
页面 - 转到页面的声明性标记
- 粘贴控件中的
<asp:Content>
文本。
若要复制源代码,请打开页面并DataModificationEvents.aspx.cs
仅复制 类中的EditInsertDelete_DataModificationEvents
文本。 复制三个事件处理程序 (Page_Load
、 GridView1_RowUpdating
和 ObjectDataSource1_Inserting
) ,但 不要 复制类声明或 using
语句。 将 复制的文本粘贴 到 EditInsertDelete_UIValidation
中的 类中 UIValidation.aspx.cs
。
将内容和代码从 DataModificationEvents.aspx
UIValidation.aspx
移动到 后,请花点时间在浏览器中测试进度。 你应该会在这两个页面中看到相同的输出并体验相同的功能, (参阅图 1 获取操作) 的 DataModificationEvents.aspx
屏幕截图。
步骤 2:将 BoundField 转换为 TemplateFields
若要将验证控件添加到编辑和插入接口,需要将 DetailsView 和 GridView 控件使用的 BoundFields 转换为 TemplateFields。 若要实现此目的,请分别单击 GridView 和 DetailsView 的智能标记中的“编辑列”和“编辑字段”链接。 在那里,选择每个 BoundFields,然后单击“将此字段转换为 TemplateField”链接。
图 3:将每个 DetailsView 和 GridView 的 BoundFields 转换为 TemplateFields (单击以查看全尺寸图像)
通过“字段”对话框将 BoundField 转换为 TemplateField 会生成一个 TemplateField,该模板字段显示与 BoundField 本身相同的只读、编辑和插入接口。 以下标记显示转换为 TemplateField 后 DetailsView 中字段的声明性语法 ProductName
:
<asp:TemplateField HeaderText="ProductName" SortExpression="ProductName">
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server"
Text='<%# Bind("ProductName") %>'></asp:TextBox>
</EditItemTemplate>
<InsertItemTemplate>
<asp:TextBox ID="TextBox1" runat="server"
Text='<%# Bind("ProductName") %>'></asp:TextBox>
</InsertItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server"
Text='<%# Bind("ProductName") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
请注意,此 TemplateField 有三个模板自动创建 ItemTemplate
、 EditItemTemplate
和 InsertItemTemplate
。 ItemTemplate
使用标签 Web 控件显示单个数据字段值 (ProductName
) ,而 EditItemTemplate
和 InsertItemTemplate
在 TextBox Web 控件中显示数据字段值,该控件使用双向数据绑定将数据字段与 TextBox 的 Text
属性相关联。 由于我们只使用此页面中的 DetailsView 进行插入,因此你可以从两个 TemplateFields 中删除 ItemTemplate
和 EditItemTemplate
,尽管保留它们没有坏处。
由于 GridView 不支持 DetailsView 的内置插入功能,将 GridView 的 ProductName
字段转换为 TemplateField 只会 ItemTemplate
生成 和 EditItemTemplate
:
<asp:TemplateField HeaderText="ProductName" SortExpression="ProductName">
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server"
Text='<%# Bind("ProductName") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server"
Text='<%# Bind("ProductName") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
通过单击“将此字段转换为 TemplateField”,Visual Studio 创建了一个 TemplateField,其模板模拟已转换的 BoundField 的用户界面。 可以通过浏览器访问此页面来验证这一点。 你会发现 TemplateField 的外观和行为与改用 BoundFields 时的体验相同。
注意
根据需要随意自定义模板中的编辑界面。 例如,我们可能需要将 TemplateFields 中的 UnitPrice
TextBox 呈现为比文本框更小的 ProductName
文本框。 为此,可以将 TextBox 的 Columns
属性设置为适当的值,或通过 属性提供绝对宽度 Width
。 在下一教程中,我们将了解如何通过将 TextBox 替换为备用数据输入 Web 控件来完全自定义编辑界面。
步骤 3:将验证控件添加到 GridView 的EditItemTemplate
构造数据输入表单时,用户必须输入任何必填字段,并且提供的所有输入都是合法且格式正确的值。 为了帮助确保用户的输入有效,ASP.NET 提供了五个内置验证控件,这些控件旨在用于验证单个输入控件的值:
- RequiredFieldValidator 确保已提供值
- CompareValidator 根据另一个 Web 控件值或常量值验证值,或确保该值的格式对于指定的数据类型是合法的
- RangeValidator 确保值在值范围内
- RegularExpressionValidator 根据正则表达式验证值
- CustomValidator 针对用户定义的自定义方法验证值
有关这五个控件的详细信息,检查 ASP.NET 快速入门教程的验证控件部分。
对于我们的教程,我们需要在 DetailsView 和 GridView 的 ProductName
TemplateFields 中使用 RequiredFieldValidator,并在 DetailsView 的 UnitPrice
TemplateField 中使用 RequiredFieldValidator。 此外,我们需要将 CompareValidator 添加到这两个控件的 UnitPrice
TemplateFields,以确保输入的价格的值大于或等于 0,并且以有效的货币格式显示。
注意
虽然 ASP.NET 1.x 具有相同的这五个验证控件,但 ASP.NET 2.0 添加了许多改进,main两个是客户端脚本支持,支持 Internet Explorer 以外的浏览器,以及能够将页面上的验证控件分区到验证组。
让我们首先将必要的验证控件添加到 EditItemTemplate
GridView 的 TemplateFields 中的 。 为此,请单击 GridView 智能标记中的“编辑模板”链接,打开模板编辑界面。 在此处,可以从下拉列表选择要编辑的模板。 由于我们想要增强编辑界面,因此需要将验证控件添加到 ProductName
和 UnitPrice
的 EditItemTemplate
。
图 4:我们需要扩展 ProductName
和 UnitPrice
的 EditItemTemplate
(单击以查看全尺寸图像)
在 ProductName
EditItemTemplate
中,通过将 RequiredFieldValidator 从工具箱拖动到模板编辑界面中,将其置于 TextBox 后面来添加它。
图 5:将 RequiredFieldValidator 添加到 ProductName
EditItemTemplate
(单击以查看全尺寸图像)
所有验证控件都通过验证单个 ASP.NET Web 控件的输入来工作。 因此,我们需要指示刚刚添加的 RequiredFieldValidator 应针对 中的 EditItemTemplate
TextBox 进行验证;这是通过将验证控件的 ControlToValidate 属性 设置为 ID
相应 Web 控件的 来实现的。 TextBox 目前具有 相当不描述 ID
的 TextBox1
,但让我们将其更改为更合适的内容。 单击模板中的 TextBox,然后在属性窗口中ID
将 从 TextBox1
更改为 EditProductName
。
图 6:将 TextBox 更改为 ID
EditProductName
(单击以查看全尺寸图像)
接下来,将 RequiredFieldValidator 的 ControlToValidate
属性设置为 EditProductName
。 最后,将 ErrorMessage 属性 设置为“必须提供产品名称”,将 Text 属性 设置为“*”。 Text
属性值(如果提供)是验证失败时由验证控件显示的文本。 ErrorMessage
属性值是必需的,由 ValidationSummary 控件使用;如果Text
省略属性值,则ErrorMessage
属性值也是验证控件在无效输入时显示的文本。
设置 RequiredFieldValidator 的这三个属性后,屏幕应类似于图 7。
图 7:设置 RequiredFieldValidator 的 ControlToValidate
、 ErrorMessage
和 Text
属性 (单击以查看全尺寸图像)
将 RequiredFieldValidator 添加到 后ProductName
EditItemTemplate
,剩下的就是将必要的验证添加到 。UnitPrice
EditItemTemplate
由于我们已决定,对于此页面, UnitPrice
编辑记录时是可选的,因此无需添加 RequiredFieldValidator。 但是,我们需要添加 CompareValidator,以确保 UnitPrice
(如果提供)正确格式化为货币,并且大于或等于 0。
在将 CompareValidator 添加到 UnitPrice
EditItemTemplate
之前,让我们先将 TextBox Web 控件的 ID 从 TextBox2
更改为 EditUnitPrice
。 进行此更改后,添加 CompareValidator,将其 ControlToValidate
属性设置为 EditUnitPrice
,其 ErrorMessage
属性设置为“价格必须大于或等于零,不能包含货币符号”,其 Text
属性设置为“*”。
若要指示 UnitPrice
该值必须大于或等于 0,请将 CompareValidator 的 Operator 属性 设置为 GreaterThanEqual
,将其 ValueToCompare 属性 设置为“0”,将其 Type 属性 设置为 Currency
。 以下声明性语法显示了 UnitPrice
在进行这些更改后 TemplateField 的 EditItemTemplate
:
<EditItemTemplate>
<asp:TextBox ID="EditUnitPrice" runat="server"
Text='<%# Bind("UnitPrice", "{0:c}") %>'
Columns="6"></asp:TextBox>
<asp:CompareValidator ID="CompareValidator1" runat="server"
ControlToValidate="EditUnitPrice"
ErrorMessage="The price must be greater than or equal to zero and
cannot include the currency symbol"
Operator="GreaterThanEqual" Type="Currency"
ValueToCompare="0">*</asp:CompareValidator>
</EditItemTemplate>
进行这些更改后,在浏览器中打开页面。 如果在编辑产品时尝试省略名称或输入无效的价格值,文本框旁边会显示星号。 如图 8 所示,包含货币符号(例如 19.95 美元)的价格值被视为无效。 CompareValidator 的 Currency
Type
允许数字分隔符 (,例如逗号或句点,具体取决于区域性设置) 和前导正号或减号, 但不允许货币 符号。 此行为可能会使用户感到困惑,因为编辑界面当前使用货币格式呈现 UnitPrice
。
注意
回想一下,在 与插入、更新和删除关联的事件 教程中,我们将 BoundField 的 DataFormatString
属性设置为 {0:c}
,以便将其设置为货币。 此外,我们将 属性设置为 ApplyFormatInEditMode
true,导致 GridView 的编辑界面将 UnitPrice
格式设置为货币。 将 BoundField 转换为 TemplateField 时,Visual Studio 会记录这些设置,并使用数据绑定语法 <%# Bind("UnitPrice", "{0:c}") %>
将 TextBox 的 Text
属性格式化为货币。
图 8:“输入无效”的文本框旁边显示星号 (单击以查看全尺寸图像)
虽然验证按原样工作,但用户在编辑记录时必须手动删除货币符号,这是不能接受的。 若要解决此问题,我们有三个选项:
EditItemTemplate
配置 ,使UnitPrice
值不会格式化为货币。- 允许用户输入货币符号,方法是删除 CompareValidator 并将其替换为用于正确检查正确格式的货币值的 RegularExpressionValidator。 此处的问题是,用于验证货币值的正则表达式并不美观,如果我们想要合并区域性设置,则需要编写代码。
- 完全删除验证控件,并依赖于 GridView
RowUpdating
事件处理程序中的服务器端验证逻辑。
让我们在本练习中使用选项 #1。 UnitPrice
由于 中 EditItemTemplate
TextBox 的数据绑定表达式,当前 已格式化为货币。 <%# Bind("UnitPrice", "{0:c}") %>
将 Bind 语句更改为 Bind("UnitPrice", "{0:n2}")
,这会将结果的格式设置为精度为两位数的数字。 这可以通过声明性语法直接完成,也可以单击 TemplateField (图 9 和图 10) 中 TextBox 中的UnitPrice
EditItemTemplate
“编辑数据绑定”链接EditUnitPrice
。
图 9:单击 TextBox 的“编辑数据绑定”链接 (单击以查看全尺寸图像)
图 10:在 语句中 Bind
指定格式说明符 (单击以查看全尺寸图像)
进行此更改后,编辑界面中的格式化价格包括逗号作为组分隔符和句点作为小数分隔符,但忽略了货币符号。
注意
UnitPrice
EditItemTemplate
不包括 RequiredFieldValidator,允许进行回发并启动更新逻辑。 但是,RowUpdating
从检查与插入、更新和删除关联的事件教程中复制的事件处理程序包括一个编程检查,UnitPrice
可确保提供 。 请随意删除此逻辑,将其保留原样,或将 RequiredFieldValidator 添加到 。UnitPrice
EditItemTemplate
步骤 4:汇总数据输入问题
除了五个验证控件外,ASP.NET 还包括 ValidationSummary 控件,该控件显示 ErrorMessage
检测到无效数据的验证控件的 。 此摘要数据可以作为文本显示在网页中,也可以通过模式的客户端消息框显示。 让我们对本教程进行改进,以包含一个客户端消息框,其中汇总了任何验证问题。
为此,请将 ValidationSummary 控件从工具箱拖到Designer。 验证控件的位置并不重要,因为我们要将它配置为仅将摘要显示为消息框。 添加控件后,将其 ShowSummary 属性 设置为 false
,将其 ShowMessageBox 属性 设置为 true
。 添加此内容后,任何验证错误都会在客户端消息框中汇总。
图 11:验证错误汇总在 Client-Side 消息框中 (单击以查看全尺寸图像)
步骤 5:将验证控件添加到 DetailsView 的InsertItemTemplate
本教程剩下的就是将验证控件添加到 DetailsView 的插入界面。 将验证控件添加到 DetailsView 的模板的过程与步骤 3 中检查的过程相同;因此,我们将在此步骤中轻松完成任务。 正如我们使用 GridView 的 EditItemTemplate
一样,我鼓励你将 TextBox 的 从非说明TextBox1
重命名ID
为 InsertProductName
和 TextBox2
InsertUnitPrice
。
将 RequiredFieldValidator 添加到 。ProductName
InsertItemTemplate
将 ControlToValidate
设置为 ID
模板中 TextBox 的 ,将其 Text
属性设置为“*”,将其 ErrorMessage
属性设置为“必须提供产品名称”。
UnitPrice
由于添加新记录时此页面需要 ,因此请向 UnitPrice
InsertItemTemplate
添加 RequiredFieldValidator,相应地设置其 ControlToValidate
、 Text
和 ErrorMessage
属性。 最后,还向 UnitPrice
InsertItemTemplate
添加 CompareValidator,配置其 ControlToValidate
、Text
、、Type
ErrorMessage
、 Operator
和 ValueToCompare
属性,就像我们在 GridView 的 EditItemTemplate
中对 UnitPrice
的 CompareValidator 所做的那样。
添加这些验证控件后,如果新产品的名称未提供,或者其价格为负数或格式非法,则无法将新产品添加到系统中。
图 12:验证逻辑已添加到 DetailsView 的插入界面 (单击以查看全尺寸图像)
步骤 6:将验证控件分区到验证组中
我们的页面由两组逻辑上不同的验证控件组成:与 GridView 的编辑接口对应的控件和与 DetailsView 的插入接口相对应的控件。 默认情况下,当发生回发时,将检查页面上 的所有 验证控件。 但是,在编辑记录时,我们不希望对 DetailsView 的插入接口的验证控件进行验证。 图 13 演示了用户编辑具有完全合法值的产品时当前面临的两难境地,单击“更新”会导致验证错误,因为插入界面中的名称和价格值为空。
图 13:更新产品导致插入接口的验证控件触发 (单击以查看全尺寸图像)
ASP.NET 2.0 中的验证控件可以通过其 ValidationGroup
属性划分为验证组。 若要在组中关联一组验证控件,只需将其 ValidationGroup
属性设置为相同的值。 在我们的教程中,将 ValidationGroup
GridView 的 TemplateFields 中的验证控件的属性设置为 EditValidationControls
, ValidationGroup
将 DetailsView 的 TemplateFields 的属性设置为 InsertValidationControls
。 这些更改可以直接在声明性标记中完成,也可以在使用Designer的编辑模板界面时通过属性窗口完成。
除了验证控件外,ASP.NET 2.0 中的 Button 和 Button 相关控件还包括 属性 ValidationGroup
。 仅当具有相同属性设置的 Button 触发回发时,才会检查验证组的 ValidationGroup
验证程序的有效性。 例如,若要使 DetailsView 的“插入”按钮触发 InsertValidationControls
验证组,我们需要将 CommandField 的 ValidationGroup
属性设置为 InsertValidationControls
(请参阅图 14) 。 此外,将 GridView 的 CommandField 属性 ValidationGroup
设置为 EditValidationControls
。
图 14:将 DetailsView 的 CommandField ValidationGroup
属性设置为 InsertValidationControls
(单击以查看全尺寸图像)
进行这些更改后,DetailsView 和 GridView 的 TemplateFields 和 CommandFields 应如下所示:
DetailsView 的 TemplateFields 和 CommandField
<asp:TemplateField HeaderText="ProductName"
SortExpression="ProductName">
<InsertItemTemplate>
<asp:TextBox ID="InsertProductName" runat="server"
Text='<%# Bind("ProductName") %>'></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator2"
runat="server" ControlToValidate="InsertProductName"
ErrorMessage="You must provide the product name"
ValidationGroup="InsertValidationControls">*
</asp:RequiredFieldValidator>
</InsertItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="UnitPrice" SortExpression="UnitPrice">
<InsertItemTemplate>
<asp:TextBox ID="InsertUnitPrice" runat="server"
Text='<%# Bind("UnitPrice") %>' Columns="6">
</asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator3"
runat="server" ControlToValidate="InsertUnitPrice"
ErrorMessage="You must provide the product price"
ValidationGroup="InsertValidationControls">*
</asp:RequiredFieldValidator>
<asp:CompareValidator ID="CompareValidator2" runat="server"
ControlToValidate="InsertUnitPrice"
ErrorMessage="The price must be greater than or equal to zero and
cannot include the currency symbol"
Operator="GreaterThanEqual" Type="Currency" ValueToCompare="0"
ValidationGroup="InsertValidationControls">*
</asp:CompareValidator>
</InsertItemTemplate>
</asp:TemplateField>
<asp:CommandField ShowInsertButton="True"
ValidationGroup="InsertValidationControls" />
GridView 的 CommandField 和 TemplateFields
<asp:CommandField ShowEditButton="True" ValidationGroup="EditValidationControls" />
<asp:TemplateField HeaderText="ProductName"
SortExpression="ProductName">
<EditItemTemplate>
<asp:TextBox ID="EditProductName" runat="server"
Text='<%# Bind("ProductName") %>'>
</asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1"
runat="server" ControlToValidate="EditProductName"
ErrorMessage="You must provide the product name"
ValidationGroup="EditValidationControls">*
</asp:RequiredFieldValidator>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server"
Text='<%# Bind("ProductName") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="UnitPrice" SortExpression="UnitPrice">
<EditItemTemplate>
<asp:TextBox ID="EditUnitPrice" runat="server"
Text='<%# Bind("UnitPrice", "{0:n2}") %>' Columns="6"></asp:TextBox>
<asp:CompareValidator ID="CompareValidator1" runat="server"
ControlToValidate="EditUnitPrice"
ErrorMessage="The price must be greater than or equal to zero and
cannot include the currency symbol"
Operator="GreaterThanEqual" Type="Currency"
ValueToCompare="0"
ValidationGroup="EditValidationControls">*
</asp:CompareValidator>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label2" runat="server"
Text='<%# Bind("UnitPrice", "{0:c}") %>'>
</asp:Label>
</ItemTemplate>
</asp:TemplateField>
此时,仅当单击 GridView 的“更新”按钮时,才会触发特定于编辑的验证控件,并且仅在单击“DetailsView”的“插入”按钮时才触发特定于插入的验证控件,从而解决了图 13 突出显示的问题。 但是,通过此更改,在输入无效数据时,ValidationSummary 控件不再显示。 ValidationSummary 控件还包含一个 ValidationGroup
属性,并且仅显示其验证组中这些验证控件的摘要信息。 因此,我们需要在此页中具有两个验证控件,一个用于 InsertValidationControls
验证组,一个用于 EditValidationControls
。
<asp:ValidationSummary ID="ValidationSummary1" runat="server"
ShowMessageBox="True" ShowSummary="False"
ValidationGroup="EditValidationControls" />
<asp:ValidationSummary ID="ValidationSummary2" runat="server"
ShowMessageBox="True" ShowSummary="False"
ValidationGroup="InsertValidationControls" />
有了这个附加功能,我们的教程就完成了!
总结
虽然 BoundFields 可以提供插入和编辑界面,但该接口不可自定义。 通常,我们希望将验证控件添加到编辑和插入界面,以确保用户以合法格式输入所需的输入。 为此,必须将 BoundFields 转换为 TemplateFields,并将验证控件添加到相应的模板 () 。 在本教程中,我们扩展了 检查与插入、更新和删除相关的事件 教程中的示例,将验证控件添加到 DetailsView 的插入界面和 GridView 的编辑界面。 此外,我们还了解了如何使用 ValidationSummary 控件显示摘要验证信息,以及如何将页面上的验证控件分区为不同的验证组。
正如我们在本教程中看到的,TemplateFields 允许扩充编辑和插入接口,以包含验证控件。 也可以扩展 TemplateFields 以包含其他输入 Web 控件,使 TextBox 可以替换为更合适的 Web 控件。 在下一教程中,我们将了解如何将 TextBox 控件替换为数据绑定的 DropDownList 控件,这在编辑外键 ((如 CategoryID
或 SupplierID
)表 Products
) 时很理想。
编程快乐!
关于作者
斯科特·米切尔是七本 ASP/ASP.NET 书籍的作者和 4GuysFromRolla.com 的创始人,自 1998 年以来一直在使用 Microsoft Web 技术。 Scott 担任独立顾问、培训师和作家。 他的最新一本书是 山姆斯在 24 小时内 ASP.NET 2.0。 可以在 上mitchell@4GuysFromRolla.com联系他,也可以通过他的博客(可在 中找到http://ScottOnWriting.NET)。
特别感谢
本教程系列由许多有用的审阅者查看。 本教程的主要审阅者是 Liz Shulok 和 Zack Jones。 有兴趣查看我即将发布的 MSDN 文章? 如果是,请在 处放置一行 mitchell@4GuysFromRolla.com。
反馈
https://aka.ms/ContentUserFeedback。
即将发布:在整个 2024 年,我们将逐步淘汰作为内容反馈机制的“GitHub 问题”,并将其取代为新的反馈系统。 有关详细信息,请参阅:提交和查看相关反馈