c# - How to Use a Compare Validator with DateTime.Today against TextBox.Text? -
goal: have 'enddate' textbox updates upon user changing it. want able check/validate date in enddatetextbox.text , make sure less today's date (in format ex: 4/19/2013).
i've tried 2 methods:
method one
<asp:textbox id="hiddentodaydate" visible = "false" runat="server" /> <asp:comparevalidator id="compareendtodayvalidator" operator="greaterthan" type="date" controltovalidate="hiddentodaydate" controltocompare="enddatetextbox" errormessage="'end date' must before today's date" runat="server" />
and following in page_load method:
hiddentodaydate.text = datetime.today.toshortdatestring();
method two
<asp:hiddenfield id="hiddentodaydate" runat="server" /> <asp:comparevalidator id="compareendtodayvalidator" operator="greaterthan" type="date" controltovalidate="hiddentodaydate" controltocompare="enddatetextbox" errormessage="'end date' must before today's date" runat="server" />
and following in page_load method:
hiddentodaydate.value = datetime.today.toshortdatestring();
to code savvy setting textbox
visible false prevents validator
seeing well, didn't know @ time , wanted document process. when try method two, come across following error when debugging:
control
hiddentodaydate
referencedcontroltovalidate property
of `compareendtodayvalidator cannot validated.description: unhandled exception occurred during execution of current web request. please review stack trace more information error , originated in code.
exception details: system.web.httpexception: control 'hiddentodaydate' referenced controltovalidate property of 'compareendtodayvalidator' cannot validated.
source error:
an unhandled exception generated during execution of current web request. information regarding origin , location of exception can identified using exception stack trace below.
so there means somehow achieve goal without having display datetime.today somewhere? i'd prefer keep code simple , clean possible , not use javascript, if javascript provides workaround, it. code appreciated. thanks!
after learning of the valuetocompare property due in part tim's post able search around , find question similar mine answer worked (had change asp.net compare type string): using comparevalidator control compare user input date today's date
here's code looks like:
asp.net:
<asp:comparevalidator id="compareendtodayvalidator" operator="lessthan" type="string" controltovalidate="enddatetextbox" errormessage="the 'end date' must before today" runat="server" />
.net:
(in page_load method)
compareendtodayvalidator.valuetocompare = datetime.now.toshortdatestring();
Comments
Post a Comment