Erik Porter's Blog

Life and Development at Microsoft and Other Technology Discussions

News

    Quick Validation Hack for ASP.NET

    So in the current project I'm working on, I have a calculated field in a hidden field (server control) and I wanted an actual textbox on the page to validate against that hidden value using the CompareValidator, but unfortunately, it won't compare against hidden fields (even though it's a server control).  So what's the way around this?  Instead of a hidden field, make it a TextBox Server Control and add style="display:none" onto it, so it doesn't show up and the CompareValidator will work just fine.  This is kind of silly, but it works!

    Posted: Oct 27 2004, 04:27 PM by HumanCompiler | with 9 comment(s)
    Filed under: ,

    Comments

    Richard P said:

    "Works" as long as your user is using a browser that supports CSS.

    Unless you're using client-side script to set the value of the hidden field, wouldn't it make more sense to just set the ValueToCompare property of the CompareValidator?
    # October 27, 2004 11:00 PM

    Erik Porter said:

    If someone's viewing my site with a browser that doesn't support CSS, then they don't deserve to see it and are probably used to seeing things that look ugly! ;)

    That is true, but I am in fact, giving it an initial value, but changing it on the client later based off of other conditions, so sorry to spoil your fun. :)
    # October 28, 2004 1:33 AM

    Danik said:

    Nice one Erik!  This worked well for validating selection of a date in a calendar control.  Isn't it a joke that the calendar control can't be validated out of the box!?!?!  Cheers.

    # March 12, 2007 4:03 AM

    Kerens said:

    Thanks a lot. I tried to do the same trick with visible=false but it didn't work. Your solution was a big help!

    # May 13, 2007 1:38 PM

    Ramu said:

    Thank you. Your solution worked for me.

    # October 26, 2007 1:19 AM

    Raghu Veer Gupta said:

    Thank You Very much......

    # November 20, 2007 11:43 PM

    Phil said:

    great tip - only enhancement if you know how, is if I am using the ValidatorCalloutExtender from AJAX.NET, how do I get the message to position properly?

    cheers

    # February 29, 2008 6:53 AM

    kalaisep86 said:

    thanks. worked for me...

    # November 12, 2008 12:03 PM

    Topics about Web-design | Quick Validation Hack for ASP.NET - Erik Porter’s Blog said:

    Pingback from  Topics about Web-design |   Quick Validation Hack for ASP.NET - Erik Porter’s Blog

    # May 5, 2009 7:19 PM