Hi I am also having a similar problem, I am getting this message, I am trying to validate that people are 16 or over. The value '08 April 1993' of the ValueToCompare property of 'cvDoB' cannot be converted to type 'Date'.
Hi, I'm trying to do something dynamic -- the record has a createdOn date field and an expiresOn date field, and I am trying to validate that expiresOn <= createdOn + 30 days. I tried your idea of validating against a HiddenField, but I get the error "
Control 'maxExpiresOn' referenced by the ControlToCompare property of 'cmpvExp2' cannot be validated."
The only type of field I seem to be able to compare with is a visible textbox, but I prefer not to show the user what the max expires on value is (they already know), and furthermore, I don't want them to be able to edit it!
The other thing I tried was setting the ValueToCompare in page_load.If not page Postback, but the problem there is it works fine as long as myu formview is being edited, but as soon as the user clicks the "update" button (ie. postback occurs), I get the
error
The value '' of the ValueToCompare property of 'cmpvExp2' cannot be converted to type 'Date'.
panzer
Member
187 Points
82 Posts
Re: date compare validator problem
Apr 08, 2009 10:37 AM|LINK
Guitoux
Member
41 Points
113 Posts
Re: date compare validator problem
Jun 05, 2009 05:42 PM|LINK
any idea why the following bit of code does not work?
CType(DetailsView1.FindControl("CompareValidator4"), CompareValidator).ValueToCompare = DateTime.NowGuitoux
Member
41 Points
113 Posts
Re: date compare validator problem
Jun 05, 2009 07:11 PM|LINK
Found a solution:
Create an invisible label and set it's text to current date/time. You can than point controltocompare to this label from your comparevalidator.
chandruk
Member
4 Points
2 Posts
Re: date compare validator problem
Jul 28, 2009 01:42 PM|LINK
Hi try with this....
<asp:CompareValidator ID="cvRadDatePickerDOB" runat="server" ControlToValidate="radDatePickerCertificationExpiryDate"
Display="Dynamic" EnableClientScript="true" ControlToCompare="hiddenExpiryDate" Operator="GreaterThan"
ErrorMessage="Expiration date should be future date" />
<asp:HiddenField id="hiddenExpiryDate" runat="server"/>
in codebeghind
hiddenExpiryDate.Value=System.DateTime.Now
date compare Validator
chandruk
Member
4 Points
2 Posts
Re: date compare validator problem
Jul 29, 2009 10:12 AM|LINK
y
Guitoux
Member
41 Points
113 Posts
Re: date compare validator problem
Jul 29, 2009 02:05 PM|LINK
yes, that works. thanks.
NedBalzer
Member
6 Points
9 Posts
Re: date compare validator problem
Dec 11, 2009 04:25 PM|LINK
Hi, I'm trying to do something dynamic -- the record has a createdOn date field and an expiresOn date field, and I am trying to validate that expiresOn <= createdOn + 30 days. I tried your idea of validating against a HiddenField, but I get the error "
Control 'maxExpiresOn' referenced by the ControlToCompare property of 'cmpvExp2' cannot be validated."
The only type of field I seem to be able to compare with is a visible textbox, but I prefer not to show the user what the max expires on value is (they already know), and furthermore, I don't want them to be able to edit it!
The other thing I tried was setting the ValueToCompare in page_load.If not page Postback, but the problem there is it works fine as long as myu formview is being edited, but as soon as the user clicks the "update" button (ie. postback occurs), I get the error
The value '' of the ValueToCompare property of 'cmpvExp2' cannot be converted to type 'Date'.
Here is the relevant code:
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
... <script runat="server">
'
...
Dim createDate As Date
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
If Not Page.IsPostBack Then
...
Dim FVLbl As Label
FVLbl = CType(dropboxFV.FindControl("createdOn"), Label)
createDate = CType(FVLbl.Text, Date)
Dim FVCV As CompareValidator = CType(dropboxFV.FindControl("cmpvExp2"), CompareValidator)
FVCV.ValueToCompare = DateAdd(DateInterval.Day, 30, createDate)
Else 'if postback
createDate = ViewState("createDate")
Dim FVCV As CompareValidator = CType(dropboxFV.FindControl("cmpvExp2"), CompareValidator)
FVCV.ValueToCompare = DateAdd(DateInterval.Day, 30, createDate)
End If
End Sub
...
Protected Sub Page_Prerender(ByVal Sender As Object, ByVal e As EventArgs)
ViewState("createDate") = createDate
End Sub
...
</script>
</head>
<body>
...
<p><asp:FormView ID="dropboxFV" runat="server" DataSourceID="DropboxSDS" DefaultMode="Edit" >
<EditItemTemplate>
...
Created By: <asp:Label ID="createdby" runat="server" Text='<%#Eval("createdby") %>' />
On: <asp:Label ID="createdon" runat="server" Text='<%#Eval("createdon", "{0:d}") %>' /><br />
<!-- asp:HiddenField ID="maxExpiresOn" runat="server" Value='<%#Eval("maxExpiresOn", "{0:d}") %>' / -->
Expires: <asp:TextBox id="expiresOn" runat="server" Text='<%#Bind("expireson", "{0:d}") %>' />
<img alt="Calendar" src="images/Calendar2.gif" id="expIcon" onclick="Calendar.Setup()" />
<asp:RequiredFieldValidator ID="reqExpiresOn" runat="server" ControlToValidate="expiresOn" text="(Required)" />
<asp:compareValidator id="cmpvExp1"
ControlToValidate="expireson"
Text="(Date required)"
Type="Date"
Operator="dataTypeCheck"
runat="server" />
<asp:CompareValidator ID="cmpvExp2" runat="server" ControlToValidate="expiresOn" Type="Date" Operator="LessThanEqual" Text="(30 days max)" />
...
Help!