Spread for ASP.NET 8.0 Product Documentation > Developer's Guide > Customizing with Cell Types > Using Validation Controls |
You can prevent a user from entering invalid characters in a cell by using a validation control in the Spread cell. You can validate the data when pasting to a cell or cell range by setting the NonEditModeValidation property to True when using a validation control. Set the AllowServerValidators property to True to support server validation using custom validator controls. You can also display an error message when the data is invalid with the ValidationErrorMessage property.
Initialize the validation control and then add the validation control to the Spread cell to provide validation.
You can use validation controls instead of the standard validation by setting the Validators property. The following cell types provide this option:
If the validation fails, the onErrorMessageShown event occurs and the user cannot change the active cell.
The validation is not supported if the ShowEditor property is true.
If you receive an error such as, "UnobtrusiveValidationMode requires a ScriptResourceMapping for 'jquery'" in Visual Studio 2012, then you may need to add the following information to web.config. <appSettings> |
This example creates a cell and assigns a basic validator to the cell.
Code |
Copy Code
|
---|---|
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="RequiredFieldValidator"></asp:RequiredFieldValidator> |
C# |
Copy Code
|
---|---|
protected void Page_Load(object sender, System.EventArgs e) // RequiredFieldValidator, from toolbox // CompareValidator, from toolbox // CompareValidator, from code // RangeValidator, from toolbox // RangeValidator, from code |
VB |
Copy Code
|
---|---|
Protected Sub Page_Load(sender As Object, e As System.EventArgs) ' RequiredFieldValidator, from toolbox ' CompareValidator, from toolbox ' CompareValidator, from code ' RangeValidator, from toolbox ' RangeValidator, from code |
This example supports server validation using a custom validator control.
Code |
Copy Code
|
---|---|
<FarPoint:FpSpread ID="FpSpread1" runat="server" BorderColor="Black" BorderStyle="Solid" BorderWidth="1px" Height="200" Width="400"> <commandbar backcolor="Control" buttonfacecolor="Control" buttonhighlightcolor="ControlLightLight" buttonshadowcolor="ControlDark"></commandbar> <sheets> <FarPoint:SheetView SheetName="Sheet1"></FarPoint:SheetView> </sheets> </FarPoint:FpSpread> <asp:CustomValidator ID="CustomValidator2" runat="server" ErrorMessage="Error of server side" OnServerValidate="CustomValidator2_ServerValidate"></asp:CustomValidator> |
C# |
Copy Code
|
---|---|
protected void Page_Load(object sender, EventArgs e) { if (IsPostBack) return; TextCellType txt = new FarPoint.Web.Spread.TextCellType(); txt.AllowServerValidators = true;// New property to enable server validating with validator controls txt.Validators.Add(CustomValidator2); FpSpread1.ActiveSheetView.Cells[1, 1].CellType = txt; FpSpread1.ActiveSheetView.Cells[1, 1].BackColor = Color.LightPink; } protected void CustomValidator2_ServerValidate(object source, ServerValidateEventArgs args) { int value = 0; args.IsValid = int.TryParse(args.Value, out value) && value < 10;// Accept integer number less than 10; } |
VB |
Copy Code
|
---|---|
Protected Sub Page_Load(sender As Object, e As System.EventArgs) If IsPostBack Then Return Dim txt As New FarPoint.Web.Spread.TextCellType() txt.AllowServerValidators = True 'New property to enable server validating with validator controls txt.Validators.Add(CustomValidator2) FpSpread1.ActiveSheetView.Cells(1, 1).CellType = txt FpSpread1.ActiveSheetView.Cells(1, 1).BackColor = Color.LightPink End Sub Protected Sub CustomValidator2_ServerValidate(source As Object, args As ServerValidateEventArgs) Dim value As Integer = 0 args.IsValid = Integer.TryParse(args.Value, value) AndAlso value < 10 'Accept integer number less than 10 End Sub |