ASP.NET GridView - how to enable validation declaratively

Posted by Joe on Stack Overflow See other posts from Stack Overflow or by Joe
Published on 2010-05-16T16:59:22Z Indexed on 2010/05/16 17:10 UTC
Read the original article Hit count: 161

Filed under:
|
|

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;
    }
}

© Stack Overflow or respective owner

Related posts about ASP.NET

Related posts about gridview