Server-Side Validation

This is a template that I use for implementing server-side validation. It uses the UIUtil class wich has numerous static methods that I wrote to simplify common tasks.

The button click event handler
protected void btnSave_Click(object sender, EventArgs e)
{
	StringCollection errors = new StringCollection();

	if (this.Validate(ref errors))
	{
		this.Save();
		// Close window, etc.
	}
	else
	{
		// Display the validation error(s) to the user
		UIUtil.DisplayValidationErrorDiv(this.Page, errors);
	}
}


The validation method
private bool Validate(ref StringCollection errors)
{
	bool blnRetVal = true;
	if (errors == null)
		errors = new StringCollection();

	// Validate required fields
	blnRetVal = blnRetVal & UIUtil.Validate_Required([formField], ref errors, "[errorMessage]");


	// Other Validation here.


	return blnRetVal;
}