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"; }
Hi there!
After that, the CustomValidator was properly invoked?
Best regards!
Rui
Pingback: CustomValidator not working with delete buttons? | DeveloperQuestion.com
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