Why doesn't my jQuery UI datepicker work for postbacks when it works fine on initial page loads?

I think a cleaner way to do this is to use ClientScriptManager. RegisterStartupScript .

Up vote 0 down vote favorite share g+ share fb share tw.

I've been trying to integrate the jQuery UI Datepicker into our ASP. NET WebForms application. The application uses master pages to provide a common look to all pages, and all of the content pages are built inside the ContentTemplate of an UpdatePanel.

I've created a user control to wrap the datepicker's functionality and allow setting of the minDate, maxDate, validators, etc. for each date from the codebehind at runtime. I also need to deal with the way ASP. NET generates client element IDs.

Over the last week, I've searched high and low, and found lots of tips for getting it to work. Here is a somewhat simplified version of where I'm at right now. I've left out the validators and some other markup that I don't think is related to the issue, but that's the meat of the user control--a textbox, and the jQuery script that enables the datepicker functionality.

The basic idea is to leverage ASP. NET's client IDs to: Provide a unique function name for the JavaScript function for this specific control Register that function with jQuery to enable the datepicker after the initial page load Register that same function with ASP. NET's PageRequestManager to keep the datepickers enabled after postbacks Items 1 and 2 are working great.

However, after a postback, the datepicker functionality is lost and I'm left with just a textbox. But wait! That's not all.

A given page consists of multiple panels, only one of which is visible at a time. These are selected by only having the Visible property true for the current panel. The Visible property for all of other panels are set to false.

Now get this: The datepickers on the first panel, the one displayed upon initial page load, work as expected. Those on the second panel just don't show up. Going back to the first page, the datepickers show up again.

Does anyone have any ideas where I'm going wrong? Asp.net jquery usercontrols master-pages jquery-datepicker link|improve this question edited Jan 1 '10 at 11:03 asked Dec 31 '09 at 20:56JeffK1,608812 80% accept rate.

– Chetan Sastry Dec 31 '09 at 21:16 I've never figured out how to get javascript debugging to work right, so I can't confirm that it is. However, using Fiddler, I can see that the script for the appropriate controls is being delivered along with the refresh. I'm trying out your other suggestion now... – JeffK Dec 31 '09 at 21:56.

I think a cleaner way to do this is to use ClientScriptManager. RegisterStartupScript. Javascript: function initCalendar(controlId) { $("#" + controlId).

Blahblah(.... } In your code behind (OnPreRender perhaps): Page.ClientScript. RegisterStartupScript(this.GetType(), "SomeName", string. Format("", txtBox.

ClientID)).

I'll give this a try. Looks promising. Does this eliminate the need to do the $(document).

Ready(function(){ (); }); – JeffK Dec 31 '09 at 21:39 Depends on where you put the RegisterStartupScript. Just ensure you call it on page load and on switching tabs. Then you don't need the $(document).ready.... It just puts a script block at the end of your form to be executed upon parse.

Obviously, jQuery and its plugins must be loaded by then. It doesn't probably work if you load them at the end of your page. – Chetan Sastry Dec 31 '09 at 21:56.

While $(document).ready() is ideal for one-time initialization routines, it leaves you hanging if you have code that needs to be re-run after every partial postback. The LiveQuery functionality added in jQuery v1.3+ helps with this, but only works for a limited set of functionality. For example, what if we wanted to add a jQueryUI datepicker to the TextBox in the previous example?

Adding it in $(document).ready() would work great, until a partial postback occurred. Then, the UpdatePanel’s new TextBox element would no longer have the datepicker wired up to it. This above Answer is taken from the below link.

I have tested and implemented this in one of my project. encosia.com/2009/03/25/document-ready-an....

Thanks for this. The content behind that link reads like this is really the solution I'm looking for, Unfortunately, I've had to move on to other functionality for our application. When we get the current version out and start on the next release, I'll definitely revisit the datepicker and try this out.

I'll mark it as "the answer" if it works. – JeffK Jan 29 '10 at 16:08.

When you set a control's Visible property to false, it doesn't get rendered at all. That means that the Javascript inside the invisible panels did not exist when the page was first loaded. To solve this, you can hide your panels using CSS (display: none) instead of the Visible property.

Right, I was aware of that, and thought it might be relevant, which is why I included that in my question. But I'm still not sure why the PageRequestManager doesn't run the functions to setup datepicker functionality for the user controls on the panel that is being rendered? Or does the broswer not "see" the new scripts for the new date controls after a postback?

If that's the case, I'm going to need another approach, but using the display:none option would make my pages just huge. (They're already well over 300-400K. ) – JeffK Dec 31 '09 at 23:19.

Well, Here is the solution of this problem. Below link explains why the datepicker doesn't work after ajax postback and the solution to make it work within ajax. jquerybyexample.blogspot.com/2010/08/jqu....

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions