ASP.Net - Validators

Validator controls

Problem: Validators not Validating

Sometimes a validator control will fire off and trigger an error, but then the event associated with that trigger runs anyway

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<span class="noprint">
<asp:ImageButton
ID="btnRowDelete"
runat="server"
ImageUrl="~/Icons/del.gif"
onclick="btnRowDelete_Click"
CausesValidation="true"
CommandArgument='<%# eval("aKey") %>'
OnClientClick="return confirm('Are you sure you want to delete the whole line of time entries? ')"
validationGroup="Delete"
/>
<asp:CustomValidator
ID="valbtnUpdate_PPLocked"
runat="server"
OnServerValidate="ValPayPeriodLocked"
ErrorMessage="PayPeriod has been locked"
ValidationGroup="Delete"
ValidateEmptyText="true"
/>

In this case, the validator fires an error, but then the record is deleted anyway. The person has no opportunity to see that there was an error.

TO fix this you add logic to your code to see if the page is valid. For example:

1
2
3
4
5
6
7
Protected Sub btnRowDelete_Click(sender As Object, e As ImageClickEventArgs)

Page.Validate("Delete")
If Not Page.IsValid() Then
Exit Sub
End If
:

Here I call the page.validate method, and then if not page.isvalid().