shepherdweb.com

How to Point CompareValidator’s ControlToCompare Attribute at a HiddenField

January 30th, 2007

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

  1. Create a new web project and add it to your solution. I called my project “CompareValidatorHelper”.
  2. Right click on the project and select Add and the Class…. I also named my class “CompareValidatorHelper”.
  3. 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
    {
    }
    }

  4. Build the project and add a reference to it in your web application project.
  5. Register the inherited control at the top of your page:
    <%@ Register TagPrefix="custom" Namespace="CompareValidatorHelper" Assembly="CompareValidatorHelper" %>
  6. Add the HiddenField control to your page:
    <custom:VHiddenField ID="hiddenBalance" runat="server" Value='<%# Eval("Balance") %>' />
  7. Finally, set the ControlToCompare attribute of your CompareValidator equal to the ID attribute of your HiddenField:
    <asp:CompareValidator ID="CompareValidator1" Display="Dynamic" runat="server" Type="Double" Operator="LessThanEqual" ErrorMessage="Overpayments are not allowed." ControlToCompare="hiddenBalance" ControlToValidate="txtPaymentAmount">*</asp:CompareValidator>

kick it on DotNetKicks.com

2 Comments »

  1. matt wrote,

    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

  2. Jon Reynolds (IT Contractor) wrote,

    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

Leave a comment

RSS feed for comments on this post.

TrackBack URI

Bad Behavior has blocked 679 access attempts in the last 7 days.