FormView, Delete button and CustomValidators

The FormView controls supports 3 modes: ReadOnly, Edit and Insert. The FormViews in one of the applications I develop typically have a table in each template for layout, some controls to display or edit values, and a button bar at the bottom.

The button bar is different for each of the modes: in ReadOnly, it contains an “Edit” button; for Insert, it has a “Create” button, and in Edit mode, it shows “Save”, “Cancel”, and “Delete”.

In a database application, deletion of records is usually not trivial. The database table may be part of a constraint, so the record has to be checked for references to decide whether deletion is possible.

I set up my “Delete” button with a CommandName “Delete” and a ValidationGroup “delete”, and wrote a CustomValidator (in the same ValidationGroup) to check whether there exist references to the record to be deleted.

The CustomValidator worked fine, I set the IsValid property according to the checks, but somehow, the Delete statement of the datasource was always invoked, no matter what the result of the validation was.

After a little bit of trying, I found a solution that works:

  • The Delete button property CommandName is not set in the designer
  • The Click event handler looks like this:
protected void btnDelete_Click(object sender, EventArgs e)
    if (Page.IsValid)
        btnDelete.CommandName = "Delete";

3 Responses to FormView, Delete button and CustomValidators

  1. Rui Gonçalves says:

    Hi there!

    After that, the CustomValidator was properly invoked?

    Best regards!

  2. Samuel Darteh says:

    Thank you so so so much. This has taken me more than 3 days.. Ahhh….was so much of a release…….God bless ur gud tots

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: