ASP.NET GridView - how to enable validation declaratively
- by Joe
It it possible to enable validation in an ASP.NET GridView purely declaratively?
What I've tried:
A GridView bound to an ObjectDataSource with SelectMethod and UpdateMethod defined
The GridView contains some ReadOnly BoundField columns and a TemplateField whose EditTemplate contains a TextBox and a RegularExpressionValidator that only allows numeric input in the TextBox.
The GridView also contains a CommandField with ShowEditButton=true and CausesValidation=true.
If I click on Edit, enter an invalid value, then click on Save, there is a PostBack, and an exception is thrown in the server (Input string was not in a correct format).
I can of course avoid this by adding validation code to the RowUpdating event handler on the server (see below), but is there any declarative way to force the validation to be done without adding this code?
protected void MyGridView_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
Page.Validate("MyValidationGroup");
if (!Page.IsValid)
{
e.Cancel = true;
}
}