C# 如何使用 DateTime.Today 与 TextBox.Text 的比较验证器?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16114778/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to Use a Compare Validator with DateTime.Today against TextBox.Text?
提问by Kurt Wagner
Goal:I have an 'EndDate' TextBox that updates upon the user changing it. I want to be able to check/validate the date in EndDateTextBox.Text and make sure it is less than today's date (in format ex: 4/19/2013).
目标:我有一个“EndDate”文本框,它会在用户更改它时更新。我希望能够检查/验证 EndDateTextBox.Text 中的日期并确保它小于今天的日期(格式为:4/19/2013)。
I've tried two 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 be before today's date" runat="server" />
And the following in my Page_Load method:
我的 Page_Load 方法中的以下内容:
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 be before today's date" runat="server" />
And the following in my Page_Load method:
我的 Page_Load 方法中的以下内容:
HiddenTodayDate.Value = DateTime.Today.ToShortDateString();
To the code savvy obviously setting a TextBoxvisible to false prevents the Validatorfrom seeing it as well, but I didn't know it at the time and wanted to document my process. When I try method two, I come across the following error when debugging:
对于精通代码的人来说,显然将TextBox可见设置为 false 也可以防止Validator它看到它,但我当时不知道它并想记录我的过程。当我尝试方法二时,在调试时遇到以下错误:
Control
HiddenTodayDatereferenced by theControlToValidate propertyof `CompareEndTodayValidator cannot be validated.Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.Web.HttpException: Control 'HiddenTodayDate' referenced by the ControlToValidate property of 'CompareEndTodayValidator' cannot be validated.
Source Error:
An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.
无法验证
HiddenTodayDate由ControlToValidate property`CompareEndTodayValidator引用的控件。说明:在执行当前 Web 请求期间发生未处理的异常。请查看堆栈跟踪以获取有关错误及其在代码中的来源的更多信息。
异常详细信息:System.Web.HttpException:无法验证“CompareEndTodayValidator”的 ControlToValidate 属性引用的控件“HiddenTodayDate”。
源错误:
执行当前 Web 请求期间生成了未处理的异常。可以使用下面的异常堆栈跟踪来识别有关异常来源和位置的信息。
So is there a means to somehow achieve my goal without having to display DateTime.Today somewhere? I'd prefer to keep my code as simple and clean as possible and not use Javascript, but if Javascript provides the only workaround, then so be it. Code would be greatly appreciated. Thanks!
那么有没有办法以某种方式实现我的目标而不必在某处显示 DateTime.Today?我更愿意让我的代码尽可能简单和干净,不使用 Javascript,但如果 Javascript 提供了唯一的解决方法,那就这样吧。代码将不胜感激。谢谢!
采纳答案by Kurt Wagner
After learning of the the ValueToCompare property due in part to Tim's post I was able to search around and find a question similar to mine with an answer that almost worked (had to change ASP.NET compare type to String): Using the CompareValidator control to compare user input date with today's date
在学习了部分由于 Tim 的帖子而导致的 ValueToCompare 属性后,我能够搜索并找到一个与我的问题相似的问题,其答案几乎有效(必须将 ASP.NET 比较类型更改为字符串): 使用 CompareValidator 控件将用户输入日期与今天的日期进行比较
Here's what my code looks like:
这是我的代码的样子:
ASP.NET:
ASP.NET:
<asp:CompareValidator ID="CompareEndTodayValidator" Operator="LessThan" type="String" ControltoValidate="EndDateTextBox" ErrorMessage="The 'End Date' must be before today" runat="server" />
.NET:
。网:
(In Page_Load method)
(在 Page_Load 方法中)
CompareEndTodayValidator.ValueToCompare = DateTime.Now.ToShortDateString();
回答by Tim Schmelter
You can set the ValueToCompareproperty programmatically to today:
您可以以ValueToCompare编程方式将属性设置为今天:
<asp:comparevalidator runat="server" Id="CompareEndTodayValidator"
errormessage="The date must be less than today"
controltovalidate="EndDate" type="Date" Operator="LessThan"
ValueToCompare="<%= DateTime.Today.ToShortDateString() %>" />
(not tested, if <%=doesn't work use <%#, then you have to remember to call Page.DataBind()somewhere(f.e. in Page_Load) if it is not in a databound context)
(未测试,如果<%=不工作使用<%#,那么你必须记住调用Page.DataBind()某处(fe in Page_Load),如果它不在数据绑定上下文中)

