I\'ve got a web form with a start date field. I\'ve tied a jquery datepicker to the txt field. Now when I choose a date in FF, the selected date is populated in the text box
I don't really know what the problem is, but I did notice that your does not have the ValidationGroup set, and both of your validators have that value set. You might try setting ValidationGroup="sh" in your TextBox and see if that helps.
Building upon the above answers and to provide further detail to my comment above,
Apparently in IE9+ and other browsers, you should now use dispatchEvent
to fire the change event. (Why does .fireEvent() not work in IE9?)
The OnSelect
function in the datepicker actually has 2 arguments:
An object with an id property that matches that of the textbox
mytextbox.datepicker({
defaultDate: null,
numberOfMonths: 1,
dateFormat: DisplayDateFormat,
onSelect: function (value, source) {
}
});
All the examples I saw used document.getElementById()
to retrieve the textbox, which I thought wouldn't be necessary seeing as the source object has the same id as the textbox. Upon closer examination it turns out that source is an object, not the textbox element. I found the following code resolved the problem though:
mytextbox.datepicker({
defaultDate: null,
numberOfMonths: 1,
dateFormat: DisplayDateFormat,
onSelect: function (value, source) {
var ctrl = document.getElementById(source.id);
if ("dispatchEvent" in ctrl) {
// IE9
var evt = document.createEvent("HTMLEvents");
evt.initEvent("change", false, true);
ctrl.dispatchEvent(evt);
} else if ("fireEvent" in ctrl) {
// IE7/IE8
ctrl.fireEvent("onchange");
} else {
$(ctrl).change();
}
}
});
Update: It appears that this approach is no longer working - not sure why. It stops the error from being thrown but doesn't trigger the change event.
As a date is selected, the datepicker triggers the change event on the INPUT
element, but the ASP.Net validator picks up the click event instead, with the source an A
element, and tries to find the validators on that A
element, instead of the INPUT
. This can be observed by inspecting event.srcElement
inside the validator's ValidatorOnChange
function. In browsers other than IE, event.type is 'change' and event.target is correctly the INPUT
.
While the no-op function onSelect: function() { }
prevents the error, by overriding the .change()
built-in to the datepicker's default onSelect
, it also prevents the validators from triggering. Here's a work-around for both:
onSelect: function() {
this.fireEvent && this.fireEvent('onchange') || $(this).change();
}
This uses the normal .change()
trigger except on IE, where it's required to use .fireEvent
to get the event object to be associated with the change and not the click.
It doesn't look like you're doing anything wrong since the ValidatorOnChange
code is generated for you; there's something wrong in the way it's creating its vals
object which appears to end up null on ie8.
It's been asked before, and the solution is overriding the onSelect
function with a no-op function.
This is not the only kind of validator problem out there. Here's a vaguely similar issue with the autocomplete feature.