I've suprisingly not been able to find examples on this. I'm creating
a user account setup page, and the validators work fine on all the
other fields. But now I'm creating a customvalidator (for the first
time) and it is not doing anything. The page needs to see if a
username already exists. Am I doing this right?
<asp:textbox TabIndex="1" ID="tLogin" MaxLength="40" runat="server" />
<asp:customvalidator id="Customvalidator1" runat="server"
EnableClientScript="False" Font-Bold="True" ErrorMessage="Username
already exists." ControlToValidate="tLogin"
OnServerValidate="CheckUser" Display="Dynamic"/>
void CheckUser(object source,
System.Web.UI.WebControls.ServerValidateEventArgs args)
{
TextBox b;
b = fLogin.FindControl("tLogin") as TextBox;
String lg = b.Text;
String strConn = "PROVIDER=Microsoft.Jet.OLEDB.4.0;DATA
SOURCE=" + Server.MapPath("info.mdb") + ";";
OleDbConnection Conn = new OleDbConnection(strConn);
Conn.Open();
String strSQL = "SELECT * FROM Customers WHERE Login = '" + lg
+ "'";
OleDbCommand Cmd = new OleDbCommand(strSQL, Conn);
OleDbDataReader Dr =
Cmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection);
if (Dr.HasRows)
{
args.IsValid = false;
} else args.IsValid = true;
Conn.Close();
}
bruce barker - 22 Jun 2007 20:27 GMT
does your codebehind call Page.Validate?
-- bruce (sqlwork.com)
> I've suprisingly not been able to find examples on this. I'm creating
> a user account setup page, and the validators work fine on all the
[quoted text clipped - 29 lines]
> Conn.Close();
> }
Dave - 22 Jun 2007 20:50 GMT
> does your codebehind call Page.Validate?
>
[quoted text clipped - 35 lines]
>
> - Show quoted text -
No.
Dave - 22 Jun 2007 21:00 GMT
Actually even when I set the custom event handler to just return
false, it still does not kick in with the error message. Why isn't it
doing anything? Any help would be appreciated.
Dave - 22 Jun 2007 21:51 GMT
Nevermind, I gave up on the server side customvalidator and placed the
query code in the submit button handler.
Sergey Zuyev - 22 Jun 2007 22:26 GMT
Custom Validator ServerValidate event will not fire if text is empty. By
default ValidateEmptyText property is set to False. Set it to true and try
again.

Signature
Programmer
> I've suprisingly not been able to find examples on this. I'm creating
> a user account setup page, and the validators work fine on all the
[quoted text clipped - 29 lines]
> Conn.Close();
> }