ASP.NET CustomValidation Won't be fired.

Z

zlusca

Hi, guys

I have asp.net form contain several textbox controls and one field is
userName I want to validate this field to make sure there is no other
user have the same userName before saving. so I create a
customValidator to do that. The strange thing is that customValidator
never fired the validation event.

Here is the code:
in aspx file the control is:
<DIV><SPAN class="form_label">Username</SPAN>
<SPAN class="form_field">
<asp:TextBox id="userName" Runat="server"
CssClass="NormalTextBox" MaxLength="25"></asp:TextBox>
<asp:RequiredFieldValidator id="userNameRequiredfieldvalidator"
Runat="server" Display="Static" ErrorMessage="User Name is Required
Field."
ControlToValidate="userName"
NAME="userNameRequiredfieldvalidator"></asp:RequiredFieldValidator>
<asp:CustomValidator id="userNameCustomValidator" runat="server"
Display="static" ErrorMessage="error"></asp:CustomValidator>
</SPAN></DIV>

in aspx.cs file:
private void InitializeComponent(){
this.userNameCustomValidator.ServerValidate += new
System.Web.UI.WebControls.ServerValidateEventHandler(this.userNameCustomValidator_ServerValidate);
}

private void userNameCustomValidator_ServerValidate(object source,
System.Web.UI.WebControls.ServerValidateEventArgs args)
{
args.IsValid = false;
}

I set a breakpoint inside the validation funtion, but seem like the
application never go through the function.

I could not figure out what the problem is. Please help. Thanks!
 
P

Peter Blum

If the textbox is blank, the CustomValidator will not fire. (That's why you
have a RequiredFieldValidator.)

Make sure that Page.Validate() is being called. It is automatically called
by your submit button's OnClick method just prior to calling its Click
event, unless you have CausesValidation=false.

It doesn't hurt to test things by calling userNameCustomValidator.Validate()
inside your Click event method. If it works there, it tells you that
Page.Validate() isn't called.

--- Peter Blum
www.PeterBlum.com
Email: (e-mail address removed)
Creator of "Professional Validation And More" at
http://www.peterblum.com/vam/home.aspx
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top