Set asp:CustomValidator text from codebehind

DJGray picture DJGray · Nov 13, 2012 · Viewed 7k times · Source

I have User ID and Password fields, both of which work well with the RequiredFieldValidator. Just below that, I have placed a CustomValidator, and I'm attempting to have a similar validation from the codebehind when the passwords don't match.

<asp:RequiredFieldValidator runat="server" ID="PWRequired"   
    ControlToValidate="PasswordTextbox"  
    Display="None"  
    ErrorMessage="<b>Required Field Missing</b><br />A password is required." />
<asp:CustomValidator runat="server" ID="PWMatch"
    ControlToValidate="PasswordTextbox"  
    Display="None"
    ErrorMessage="<b>Password is Invalid</b><br />Please enter a valid password." />
<AjaxControl:ValidatorCalloutExtender 
    runat="Server"
    ID="PWValidationExtender"
    TargetControlID="PWRequired" 
    Width="185px"
    WarningIconImageUrl="/Images/warning.gif" />
<asp:TextBox ID="PasswordTextbox" runat="server" CssClass="passwordEntry" TextMode="Password"></asp:TextBox>

(Hopefully that formatted correctly. I've never gotten the "4 space" thing to work.

Then in the codebehind, when the PWs don't match, I'm trying this:

CustomValidator myValidator = (CustomValidator)this.FindControl("PWMatch");
myValidator.Text = "<b>Password is Invalid</b><br />Please enter a valid password."; 
ValidatorCalloutExtender myValExtender = (ValidatorCalloutExtender)this.FindControl("PWValidationExtender");
myValExtender.ID = "PWValidationExtender";
myValExtender.TargetControlID="PWMatch";
myValExtender.WarningIconImageUrl = "/Images/warning.gif";
myValExtender.Width = new Unit(250);

I hope it is something simple and silly that I'm missing. I've burned a whole day trying to figure this out.

Thanks in advance for your help.

=========================================================================

Thanks freefaller. I've tried that and get "no joy." So, I'm coming at it from a little different direction.

I've changed to a CompareValidator, like this: (again, hope the markup works)

<asp:CompareValidator runat="server" ID="PWCompareValidator"
    ControlToValidate="PasswordTextbox"
    Operator="Equal"
    ValueToCompare="<%# GetPassword() %>"
    Text="<b>Password is Invalid</b><br />Please enter a valid password."
    Type="String" />

It's giving me a message, but always evaluating to "false" because the GetPassword method is not being called. If I keep hammering at this, I think I can get it to work, but if you see a quick solution, I'm all ears. Thanks again!!

Answer

freefaller picture freefaller · Nov 13, 2012

Instead of .Text use .ErrorMessage

So change...

myValidator.Text = "<b>Password is Invalid</b><br />Please enter a valid password."; 

To...

myValidator.ErrorMessage = "<b>Password is Invalid</b><br />Please enter a valid password."; 

Update (due to update in question)

The CompareValidator is designed to compare two values on the actual website... the ValueToCompare is supposed to be a static value, and by placing the password into it, you are opening up a security hole (because you'll be potentially sending the password to the browser - although I have doubts that ValueToCompare="<%# GetPassword() %>" will actually work in the first place.)

The CustomValidator requires you to provide at least one handler... definitely one on the server (using the OnServerValidate attribute), and if necessary one on the browser (using the ClientValidationFunction attribute). It is within the handlers that you must set the IsValid attribute of the args parameter according the result of your checks.

(I say "definitely one on the server" because client-side script is easy to get around, so you always want to have something on the server to catch anybody trying.)

It isn't 100% clear exactly what you're trying to do, or indeed exactly what you've currently got in place, so it's difficult to give you any direct advice other than what I have above.


Final Update

As you are trying to check a password, here is an example of what I think you need...

Password: <asp:TextBox runat="server" id="txtPassword"/>
<asp:RequiredFieldValidator runat="server" id="reqPassword"
   ControlToValidate="txtPassword" ErrorMessage="Provide Password" />
<asp:CustomValidator runat="server" id="cusPassword"
   ControlToValidator="txtPassword" ErrorMessage="Incorrect Password"
   OnServerValidate="cusPassword_ServerValidate"/>

Then in your code-behind, you need to have the handler

protected void cusPassword_ServerValidate(object sender, ServerValidateEventArgs args)
{
  args.IsValid = (txtPassword.Text == GetPassword());
}