[英]Cause an ASP.NET RangeValidator to fire when the value of the ControlToValidate is updated via jQuery
I have a set of date range controls: 我有一组日期范围控件:
<div id="CustomDateRow" class="row-fluid" runat="server">
<label class="span2 FieldLabel">
Start Date:
</label>
<asp:TextBox ID="StartDate" CssClass="datepicker span3" runat="server" ClientIDMode="Static"></asp:TextBox>
<label class="span2 FieldLabel">
End Date:
</label>
<asp:TextBox ID="EndDate" CssClass="datepicker span3" runat="server" ClientIDMode="Static"></asp:TextBox>
</div>
I'm converting those controls to jQuery UI date pickers. 我正在将这些控件转换为jQuery UI日期选择器。
I need to ensure that the user can't select more than 30 days at any given time. 我需要确保用户在任何给定时间不能选择超过30天。 Originally I had planned on setting the
maxDate
of the EndDate
when the StartDate
is set, but there are two basic problems with that. 最初,我计划在设置
StartDate
设置EndDate
的maxDate
,但是这样做有两个基本问题。
DateTime.AddMonths()
analogous function in JavaScript. DateTime.AddMonths()
函数。 So, I figured I'll just use a RangeValidator
and validate the number of days between the two dates every time the values change. 因此,我想我将只使用
RangeValidator
并在每次值更改时验证两个日期之间的天数。
So, I have a RangeValidator
like this: 因此,我有一个
RangeValidator
像这样:
<div class="row-fluid">
<asp:RangeValidator ID="DateRangeValidator" runat="server" ControlToValidate="DateRange"
ErrorMessage="The date range must be at least one day and not more than thirty."
EnableClientScript="true" MinimumValue="1" MaximumValue="30" CssClass="errortext span9 offset2"
Display="Dynamic" />
</div>
And it's validating a basic text input control: 并且它正在验证基本的文本输入控件:
<asp:TextBox ID="DateRange" runat="server" ClientIDMode="Static" />
Which is updated in JavaScript when the dates change: 日期更改时在JavaScript中更新的内容:
$('.datepicker').change(function () {
var nDifference = Math.abs(new Date($('#StartDate').val()) - new Date($('#EndDate').val()));
var one_day = 1000 * 60 * 60 * 24;
$('#DateRange').val(Math.round(nDifference / one_day));
});
Even though the value does change when I set it via jQuery, the RangeValidator
doesn't react. 即使当我通过jQuery设置值时确实更改了该值,
RangeValidator
也没有反应。 However, if I type the value into the text input and leave it the RangeValidator
works as expected. 但是,如果我在文本输入中键入该值并将其保留,
RangeValidator
按预期工作。
Do I need to fire some kind of event on the DateRange
control to force the RangeValidator
to execute? 我是否需要在
DateRange
控件上触发某种事件以强制RangeValidator
执行?
Well, I ended up doing two pretty simple things to solve this and it all resided in the JavaScript method to calculate the range. 好吧,我最终做了两个非常简单的事情来解决这个问题,并且所有这些问题都驻留在JavaScript方法中以计算范围。 Here is the working code.
这是工作代码。
$('.datepicker').change(function () {
var nDifference = new Date($('#EndDate').val()) - new Date($('#StartDate').val());
var one_day = 1000 * 60 * 60 * 24;
$('#DateRange').val(Math.round(nDifference / one_day));
Page_ClientValidate(null);
});
Note that I got rid of the Math.abs
on the nDifference
calculation because I needed the real difference, positive or negative. 请注意,在
nDifference
计算中我摆脱了Math.abs
,因为我需要真正的差异,即正数或负数。 Finally, note that I added the line Page_ClientValidate(null);
最后,请注意,我添加了行
Page_ClientValidate(null);
, that's what causes the RangeValidator
I setup to validate. ,这就是导致我设置的
RangeValidator
进行验证的原因。
Works perfectly! 完美的作品!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.