Topaz Filer: if you use e-mail for business, we can save you money and decrease your risk.
Validating against the value in a hidden input
Messages   Related Types
This message was discovered on microsoft.public.dotnet.framework.aspnet.webcontrols.
Responses highlighted in red are from those people who are likely to be able to contribute good, authoratitive information to this discussion. They include Microsoft employees, MVP's and others who IMHO contribute well to these kinds of discussions.

Jonathan Levine
Hello... excuse the dumb question, but I would like to validate the text in a textbox by comparing it to the text in a hidden input control. (I set the value of the hidden input control via JScript).

I have tried this:

<asp:TextBox id="CompareTextbox" runat="server" Width="122px" TextMode="Password"></asp:TextBox>
<input id="CheckHidden" runat="server" Type="Hidden"/>
<asp:CompareValidator id="CompareValidator" runat="server" ControlToValidate="CompareTextbox"
Display="Dynamic" ControlToCompare="CheckHidden">No Match</asp:CompareValidator>

But I get a Server Error: Control 'CheckHidden' referenced by the ControlToCompare property of 'CompareValidator' cannot be validated

What am I missing?
Reply to this message...
Vote that this is a GOOD answer...
 
Auto-following on Twitter
Ubuntu and XP on one “desktop”
 
    
Steven Cheng[MSFT] (VIP)
Hi,

From your description, you're using compare validation control to validate
a certain asp.net TExtBox control's value
and he set the ControlToCompare property to a HtmlHiddenField control and
get error at runtime,yes?

I think this is a normal behavior since the HtmlInputHIdden field is not
the acceptable control to for being used by the CompareValidator. The
"ControlToValidate" is generally set to the Id of another asp.net textbox
control or HtmlInputText Control but not HtmlInputHidden control. In fact,
we can verify this by pull the dropdownlist of the ControlToValidate
property in VS.NET IDE's property window. it'll list out all the available
controls to be set.

In addition, as for your situation, I think we can use CustomValidator
control or manually use javascript function to compare the values in the
two controls. How do you think of this? If you have any other ideas, please
also feel free to post here.
Thanks.

Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Get Preview at ASP.NET whidbey
http://msdn.microsoft.com/asp.net/whidbey/default.aspx

Reply to this message...
Vote that this is a GOOD answer...
 
 
    
Jonathan Levine
Hi Steven,

"Steven Cheng[MSFT]" wrote:
[Original message clipped]

That's right.

[Original message clipped]

That's too bad. I think it would be a useful enhancement.

[Original message clipped]

Actually, I took an idea from KB article 310082 and subclassed the HtmlInputHidden control. All I needed to do was add the ValidationProperty and everything works fine, e.g.:

using System;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.ComponentModel;

namespace ClientSideControls
{
    /// <summary>
    /// Summary description for HiddenInput.
    /// </summary>
    [DefaultProperty("Value"),
        ValidationProperty("Value"),
        ToolboxData("<{0}:HiddenInput runat=server></{0}:HiddenInput>")]
    public class HiddenInput : HtmlInputHidden
    {
    }
}

Regards,

Jonathan
Reply to this message...
Vote that this is a GOOD answer...
 
 
    
Steven Cheng[MSFT] (VIP)
Hi Jonathan,

Thanks very much for the response. And your solution that use the
"ValidationProperty" according to the Kb article is so cool. I must say
that is the best solution for this issue:). Also, thanks again for sharing
your code sample with us. Have a good day!

Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Get Preview at ASP.NET whidbey
http://msdn.microsoft.com/asp.net/whidbey/default.aspx

Reply to this message...
Vote that this is a GOOD answer...
 
 
 
System.Web.UI.HtmlControls.HtmlInputHidden
System.Web.UI.HtmlControls.HtmlInputText
System.Web.UI.MobileControls.CompareValidator
System.Web.UI.MobileControls.CustomValidator
System.Web.UI.MobileControls.TextBox
System.Web.UI.WebControls.CompareValidator
System.Web.UI.WebControls.CustomValidator
System.Web.UI.WebControls.TextBox
System.Windows.Forms.TextBox




Ad
BootFX
Reliable and powerful .NET application framework.
Recession Busting Bespoke Software
Get through the recession by investing in bespoke software to decrease costs and create commercial opportunities.
Other DN247 Network Sites
.NET 247
SQL Server Wins
Old Skool Developer
 
Copyright © AMX Software Ltd 2008-2009. Portions copyright © Matthew Baxter-Reynolds 2001-2009. All rights reserved.
Contact Us - Terms of Use - Privacy Policy - .NET 247 is a member of the DN247 Network - 4.0.30129.1734