I've got a very interesting bug with a jQueryUI datepicker and an UpdatePanel where the datepicker's chosen date is about 100 years off. jQuery is version 1.6.2, and jQueryUI is version 1.8.14. Here's the rough layout:
<asp:UpdatePanel ID="myUpdatePanel" runat="server">
<ContentTemplate>
<asp:DropDownList ID="myDdl" runat="server" AutoPostBack="true">
<asp:ListItem Value="foo" Text="Foo" />
<asp:ListItem Value="bar" Text="Bar" />
</asp:DropDownList>
<asp:TextBox ID="myText" runat="server" CssClass="dateText" />
</ContentTemplate
</asp:UpdatePanel>
<script type='text/javscript'>
$(document).ready(function()
{
$('.dateText').datepicker({ changeYear: true, showButtonPanel: true, yearRange: '-2:+2' });
}
function pageLoad(sender, args)
{
if (args._isPartialLoad == true)
{
$('.dateText').datepicker({ changeYear: true, showButtonPanel: true, yearRange: '-2:+2' });
}
}
</script>
So I've got an UpdatePanel that contains a dropdown that causes a postback when it's changed, as well as a text box. I've got jQueryUI setting the textbox up as a datepicker. Note that all this is contained in a jQueryUI modal dialog.
So here's what happens:
It seems that the datepicker gets confused when it's initialized when it's already open after the partial postback. I know that the datepicker setup doesn't survive the partial postback and that I need to reinitialize it in the pageLoad
function.
I can tell if this is happening by checking if $('.ui-datepicker-title').length
is greater than zero when setting up the datepicker in pageLoad
. But I don't know what to do from there. After verifying that it's happening, I've tried the following in pageLoad
before setting up the datepicker:
$('.dateText').unbind();
$('.dateText').datepicker('destroy');
$('#ui-datepicker-div').remove();
It either does the same thing or closes itself and doesn't come up at all anymore.
How can I fix this?
Update: I tried it with jQueryUI 1.8.18 and jQuery 1.7.1 (latest versions at time of writing) and the problem still happens.
Update 2: I've managed to work around the problem by supplying a function in the options for onChangeMonthYear
when the datepicker is initialized in pageLoad
. If the year parameter of that event is either 1899 or 1900, I set the datepicker's date to one month back or forward from now (respectively). That sets the textbox's content, which I don't want, so I then set the value of the textbox to ''. The net result is that the datepicker acts normally.
I'd still like to understand this whole problem better, though, so I know if it's a bug in jQueryUI that I should file or whether I should be doing something different elsewhere.
Update 3: Still happening with ASP.NET 4, jQuery 1.7.2 and jQueryUI 1.8.21. I tested it on an otherwise blank page with a setup just like what's described here, and saw the same behavior. I'm calling this a jQueryUI bug. It's filed here.
UPDATE 4: I've reproduced the problem without ASP.NET in this jsfiddle.
According to comments on the jQueryUI bug I filed, this is not a situation that should ever really happen. But I think it's going to, so I'm going to post my workaround here for future reference.
Given an input with ID myInput
:
var changingDate = false;
$('#myInput').datepicker({onChangeMonthYear:function(year, month, inst)
{
if (changingDate == false)
{
changingDate = true; //I set the date later which would call this
//and cause infinite recursion, so use this flag to stop that
if (year == 1899 || year == 1900)
{
var now = new Date(); //what the picker would have had selected
//before clicking the forward/backward month button
if (year == 1899) //I clicked backward
{
now.setMonth(new.getMonth() - 1);
}
else if (year == 1900) //I clicked forward
{
now.setMonth(now.getMonth() + 1);
}
$(this).datepicker('setDate', now);
}
changingDate = false;
}
}
});
Use the onChangeMonthYear
option shown above when initializing the picker from the pageLoad
function:
function pageLoad(sender, args)
{
if (args._isPartialLoad == true) //called because of UpdatePanel refresh
{
//set up datepicker as shown above
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With