views:

360

answers:

3

Hi there,

I have an update panel which contains a table, to which I add rows of controls on a button click. One of the controls that is added, is a user control and it is a datepicker. Inside of that user control I have a textbox, and I have JQuery which applies the JQuery UI DatePicker plugin to it thereby turning it into a datepicker. It's not a problem if that user control is loaded onto a page dynamically, however, if it is done on async postbacks inside an update panel, the javascript doesn't fire and therefore the textbox is render but without all the jquery datepicker functionality. Here's some code that's inside the DatePicker.ascx:

$(function() {
       //reset the localization
        $.datepicker.setDefaults($.extend($.datepicker.regional['']));
        $("#<%=txtDate.ClientID%>").datepicker({ dateFormat: 'mm/dd/yy', showOn: 'button', buttonImage: '/images/calendar.gif', buttonImageOnly: true, altField: '#<%=txtDate.ClientID%>' });

}

So this jquery isn't fired when the control is loaded in dynamically on an async postback. So how can I make this work?

+1  A: 

When you render a script in a response, it does just that: render the script. It is during the rendering of the page that the script is first rendered, and that has already happened by the time that this code is being executed.

Microsoft has come up with a solution for this, which is to register the script block via ScriptManager.RegisterClientScriptBlock, and the ScriptManager will then see to it that the script is executed after page load.

Depending on your situation, that may or may not be helpful. Or, in the intermediate: it may be helpful only with a bit of workarounds. From your comment, I can think of one workaround I might have considered using:

Add a GetClientScript method to your UserControl, where the JavaScript is returned. If the UserControl is loaded on a regular page load, you invoke that method at the point where you are currently rendering your script. If the control is loaded in a UpdatePanel postback, you get the scriptbody from the control using that method, and register it in the ScriptManager at hand. You should, in this case, also pass a parameter back to the UserControl not to register the script once more, where it usually does.

David Hedlund
How do I do that? That script is part of the user control which may not always be loaded into a page with update panel and script manager.
I completely edited my reply to accommodate your request. hope that helps
David Hedlund
yes thanks, I think I'll have to do it this way. Web development is still crappy as ever. I wish things would just play well together.
A: 

Is the the DatePicker.ascx inserting that javascript at the top of the page during the initial load? And then that function fails since the datepicker is not loaded yet. And then you load the datepicker on async into the update panel, but the function is not called.

Jeff Widmer
+2  A: 

Check this Page look for the session on DatePicker it says that ASP.NET AJAX UpdatePanel's wipes the DOM, so the load doesn't fire. the post says you can either, use the EndRequest handler, or the pageLoad shortcut, however I'm thinking if you just couldn't register the javascript with ScriptManager.

Hope this helps.

Gabriel Guimarães
thanks, but pageLoad method won't work since my javascript is part of the user control, and the user control is loaded within the updatepanel. Thanks for the info though, very interesting.