Developing A Form WebPart – Part 4 – Setup the Validation Controls
The data that is being submitted with the Forms WebPart requires validation before it is committed to the database in order to maintain integrity of the information being stored. Doing validation with WebParts typically occurs in two main ways. One can choose to script a separate method in order to validate the entry by using an option like ServerValidateEventArgs.IsValid against the argument, or the inherent validation controls that ASP.NET 2.0 provides can be called in order to validate the information. Considering software architecture concerns and code bloat as a factor, it is typically preferable to use the inherent ASP.NET 2.0 validation options, which will also allow the extension of the validation being used to be assimilated by sister validation controls such as ValidationSummary.
Here, you’re validating a numeric entry manually with a custom method.
- public void ValidateNumeric(object source, ServerValidateEventArgs args)
- Decimal temp = Decimal.Parse(args.Value);
- args.IsValid = true;
- args.IsValid = false;
And now, validate input using the native validation controls.
- protected override void CreateChildControls()
- textTextBox = new TextBox();
- textTextBox.ID = "textTextBox";
- rfvFieldValidator = new rfvFieldValidator();
- rfvFieldValidator.ControlToValidate = "textTextBox";