How to Point CompareValidator’s ControlToCompare Attribute at a HiddenField
If you point the ControlToCompare attribute of the CompareValidator at a HiddenField you’ll get an error that looks something like this:
Control ‘hiddenBalance’ referenced by the ControlToCompare property of ‘CompareValidator1′ cannot be validated.
With a small amount of “googling” I found two useful articles:
Between these two article I pieced together a solution.
The Solution
- Create a new web project and add it to your solution. I called my project “CompareValidatorHelper”.
- Right click on the project and select Add and the Class…. I also named my class “CompareValidatorHelper”.
- Paste the following C# code into the class:
using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.ComponentModel;
namespace CompareValidatorHelper
{
[DefaultProperty("Value"), ValidationProperty("Value"), ToolboxData("<{0}:VHiddenField runat=server></{0}:VHiddenField>")]
public class VHiddenField : System.Web.UI.WebControls.HiddenField
{
}
}
- Build the project and add a reference to it in your web application project.
- Register the inherited control at the top of your page:
<%@ Register TagPrefix="custom" Namespace="CompareValidatorHelper" Assembly="CompareValidatorHelper" %> - Add the
HiddenFieldcontrol to your page:
<custom:VHiddenField ID="hiddenBalance" runat="server" Value='<%# Eval("Balance") %>' /> - Finally, set the
ControlToCompareattribute of yourCompareValidatorequal to theIDattribute of yourHiddenField:
<asp:CompareValidator ID="CompareValidator1" Display="Dynamic" runat="server" Type="Double" Operator="LessThanEqual" ErrorMessage="Overpayments are not allowed." ControlToCompare="hiddenBalance" ControlToValidate="txtPaymentAmount">*</asp:CompareValidator>
I was able to bypass this error by changing my hidden items (I was using two asp:labels) and making them asp:textboxes instead. My problem was due to the fact that asp.net doesn’t know which field to validate when using a label, but a textbox is easy (it’s the text field).
Don’t know if that was the same issue you had, but thanks for the insite.
Comment on May 4, 2007 @ 4:20 pm
I resolved this by keeping the hidden field as before but adding a custom validator to compare the hidden field value.
*
and in javascript added the following func:
function comparefunc(sender, args)
{
var hid = document.getElementById(“”);
var text = document.getElementById(“”);
if (hid.value == text.value)
{
//values have not changed
args.IsValid = true;
return;
}
args.IsValid = false;
return;
}
Make sense??
Comment on September 23, 2008 @ 5:10 am
This did not work for me. The field name (in the example it would be “hiddenBalance” could not be found.
No difference for me, really.
Comment on October 24, 2010 @ 8:53 pm
@Paul – This article was written back in 2007, the MVC framework has changed quite a bit since then. Sorry you didn’t find what you needed.
Comment on October 25, 2010 @ 11:39 am