I have both a required field validator and custom validator for validating a texbox. The required field validator fires perfectly. I'm not able to get the custom validator to fire properly?
<asp:TextBox ID="txtPRI" runat="server" Width="295" /><br />
<asp:RequiredFieldValidator display="Dynamic" CssClass="leftAlign" SetFocusOnError="true" runat="server" controltovalidate="txtPRI" errormessage="Please enter your PRI" />
<asp:CustomValidator runat="server" id="cusCustom" controltovalidate="txtPRI" onservervalidate="cusCustom_ServerValidate" Enabled="true" ValidateEmptyText="true" display="Dynamic" CssClass="leftAlign" SetFocusOnError="true" errormessage="The text must be exactly 8 characters long!" />
code behind
protected void cusCustom_ServerValidate(object sender, ServerValidateEventArgs e)
{
Response.Write("firing - test");
Response.End();
if (e.Value.Length == 8)
e.IsValid = true;
else
e.IsValid = false;
}
The problem is that you're calling
Response.End()
which effectively stops all execution of the page. Thus, the if/else block isn't being run at all. Comment that line out or skip over it while debugging and the validator will fire as expected.I suggest you use a debugger instead of writing responses out in this fashion or be aware of the consequences of
Response.End()
if you choose to use it.