添加 Cell 验证
Contents
[
Hide
]
Aspose.Cells.GridWeb 的高级功能之一是为单元格添加输入验证规则。开发人员可以为单元格创建不同类型的验证规则来控制和验证输入值。本主题讨论支持的验证类型以及如何创建它们。
验证类型
使用 Aspose.Cells.GridWeb 可以应用三种类型的验证:
- 列表验证。
- 下拉列表验证。
- 自定义表达式验证。
下面详细讨论每一个。
列表验证
列表验证允许用户通过键入或从菜单中选择值来提供单元格输入。要为单元格创建列表验证:
- 将 Aspose.Cells.GridWeb 控件添加到 Web 窗体。
- 访问工作表。
- 访问要向其添加验证的单元格。
- 为单元格创建验证并将验证类型指定为列表。
- 为列表验证添加值。
示例代码将列表验证添加到 C1。当用户单击该单元格时,将出现一个列表。
输出:从列表中选择一个值
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
// Accessing the cells collection of the worksheet that is currently active | |
GridWorksheet sheet = GridWeb1.WorkSheets[GridWeb1.ActiveSheetIndex]; | |
// Access "B1" cell and add some text | |
GridCell cell = sheet.Cells[0, 1]; | |
cell.PutValue("Select Course:"); | |
// Accessing "C1" cell | |
cell = sheet.Cells[0, 2]; | |
// Creating List validation for the "C1" cell | |
var validation = cell.CreateValidation(GridValidationType.List, true); | |
// Adding values to List validation | |
var values = new System.Collections.Specialized.StringCollection(); | |
values.Add("Fortran"); | |
values.Add("Pascal"); | |
values.Add("C++"); | |
values.Add("Visual Basic"); | |
values.Add("Java"); | |
values.Add("C#"); | |
validation.ValueList = values; |
下拉列表验证
下拉列表验证允许用户通过从预定义列表中选择一个值来为单元格提供输入。要创建下拉列表验证:
- 将 Aspose.Cells.GridWeb 控件添加到 Web 窗体。
- 访问工作表。
- 访问要为其创建验证的单元格。
- 为单元格创建验证并将类型指定为 DropDownList。
- 添加验证值。
示例代码向 C1 添加了一个下拉列表验证。当用户单击单元格时,会出现一个下拉列表,用户可以从中选择一个值。
从下拉列表中选择一个值
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
// Accessing the cells collection of the worksheet that is currently active | |
GridWorksheet sheet = GridWeb1.WorkSheets[GridWeb1.ActiveSheetIndex]; | |
// Access "B1" cell and add some text | |
GridCell cell = sheet.Cells[0, 1]; | |
cell.PutValue("Select Degree:"); | |
// Accessing "C1" cell | |
cell = sheet.Cells[0, 2]; | |
// Creating DropDownList validation for the "C1" cell | |
var validation = cell.CreateValidation(GridValidationType.DropDownList, true); | |
// Adding values to DropDownList validation | |
var values = new System.Collections.Specialized.StringCollection(); | |
values.Add("Bachelor"); | |
values.Add("Master"); | |
values.Add("Doctor"); | |
validation.ValueList = values; |
自定义表达式验证
自定义表达式验证允许开发人员编写自己的自定义正则表达式来验证输入值。要创建自定义表达式验证:
- 将 Aspose.Cells.GridWeb 控件添加到 Web 窗体。
- 访问工作表。
- 访问要为其创建验证的单元格。
- 为单元格创建验证并将类型指定为 CustomExpression。
- 设置验证的正则表达式。
示例代码向 C1 添加自定义表达式验证。用户只能按照正则表达式指定的格式将日期添加到单元格中。
根据正则表达式向 C1 添加日期值
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
// Accessing the cells collection of the worksheet that is currently active | |
GridWorksheet sheet = GridWeb1.WorkSheets[GridWeb1.ActiveSheetIndex]; | |
// Access "B1" cell and add some text | |
GridCell cell = sheet.Cells[0, 1]; | |
cell.PutValue("Date (yyyy-mm-dd):"); | |
// Access "C1" cell and add to it custom expression validation to accept dates in yyyy-mm-dd format | |
cell = sheet.Cells[0, 2]; | |
var validation = cell.CreateValidation(GridValidationType.CustomExpression, true); | |
validation.RegEx = @"\d{4}-\d{2}-\d{2}"; |
强制验证
使用 Aspose.Cells.GridWeb,用户可以将输入数据发布到服务器。即使不同单元格有校验规则,但GridWeb控件的ForceValidation属性没有设置为true,输入的数据错误也会提交给服务器,不强制校验。默认情况下,GridWeb 的 ForceValidation 属性始终设置为 true。
当 ForceValidation 属性为真时,控件不会将数据发布到 Web 服务器,直到所有单元格的输入值都有效。例如,如果有人在单元格中输入了无效的输入值,或者没有输入值,则会激活客户端验证,用户即使点击也无法发布数据提交.
GridWeb 突出显示的错误输入值