简体   繁体   English

通过jQuery更新ControlToValidate的值时,导致ASP.NET RangeValidator触发

[英]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设置EndDatemaxDate ,但是这样做有两个基本问题。

  1. There's no good DateTime.AddMonths() analogous function in JavaScript. JavaScript中没有类似的好的DateTime.AddMonths()函数。
  2. This SO article made me realize that it's really much less user friendly. 这篇SO文章使我意识到,它确实不那么用户友好。

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));
});

The Problem 问题

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.

相关问题 jQuery-获取.NET的值RequiredFieldValidator“ ControlToValidate” - jQuery - Get value of .NET RequiredFieldValidator “ControlToValidate” 在asp.net中更新字段时使用jQuery Highlight - Using jQuery Highlight when a field is updated in asp.net 如何通过 jquery 或 java 脚本触发 asp.net 按钮事件? - How can I fire an asp.net button event via jquery or java script? 如果通过ajax加载部分,则不会触发ASP.NET MVC Jquery验证 - ASP.NET MVC Jquery validation won't fire if partial is loaded via ajax 通过jQuery使用ASP.NET MVC提交表单并接收值 - Submitting a form with ASP.NET MVC via jQuery and receiving a value 通过 jquery 获取 asp.net CheckBox 的值 - Get value of asp.net CheckBox via jquery 通过jquery获取asp.net CheckBox的值(true / false) - Get value of asp.net CheckBox via jquery(true/false) jQuery停止通过ASP.NET MVC中的Ajax在更新的页面上工作 - jQuery stop working on updated page via Ajax in ASP.NET MVC 在异步操作期间通过jQuery ajax更新ASP.net进度栏 - ASP.net progress bar updated via jQuery ajax during async operation Asp.Net数据属性通过JS / jQuery动态更新,但通过后面的代码检索时更新的属性不会更新 - Asp.Net data attributes dynamically updated through JS/jQuery but the updated attributes aren't updated when retrieved through code behind
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM