views:

27

answers:

1

In a ListView, I have a CustomValidator set up to validate a field whenever a button with CommandName="Delete" is clicked.

<ItemTemplate>
    <asp:TextBox ID="NameTextBox" Text=<%# Eval("Name") %> runat="server" />
    <asp:Button ID="DeleteButton" Text="Delete" CommandName="Delete" ValidationGroup="Delete" runat="server" />
    <asp:CustomValidator ValidationGroup="Delete" SetFocusOnError="true" Display="Dynamic" OnServerValidate="CustomValidator_ServerValidate" runat="server">You can't delete this.</asp:CustomValidator>
</ItemTemplate>

However, the error message is never displayed and the processing continues. What's strange is that the custom validation method is called, finds the field, and properly sets up e.IsValid to false. It does not matter whether I check Page.IsValid or not, because the error message is not displayed anyway.

It works if I remove the CommandName="Delete" from the button.

With Google I found the following solution, which seems to indicate someone has had a similar issue:

http://devio.wordpress.com/2007/12/11/formview-delete-button-and-customvalidators/

But I want to make sure that this solution is the way to go. I mean, is custom validation really not supposed to work with a delete button in a databound control, seriously?

A: 

I've alrealy heard about a problem like that, he solved it by doing it entirely differently. Like, instead of the customValidator, he puts a Label set EnableViewState="False" and Visible="False" and he check on the delete event the conditions and put the response back into the label. Maybe it can't works for you too?

But, if you realy ask "Why?????", I know he didn't find the exact reason ...

Simon