So I did some reading of the related questions and had some interesting stuff but did not find my answer, at least did not understand the answer.
I am very new to AJAX, javascript and sclient side scripting in general.
I have been using C# asp.net for a bit and recently added some updatepanels to my side to smooth so of the user controls and bits being updated so that the page was not reloaded each time. All works brilliantly and I was very happy with it till I decided to try and use some JQuery.
I have picked up the datepicker from ui.jquery.js which is cool and works great on a normal page. My problem arrives when I do a postback from within an updatepanel. The datepicker just stops working.
from what I have read I need to manually wire this back up after the post back.
1) I don't really understand why. on my master page I have:
<script type="text/javascript">
$(function() {
$(".mydatepickerclass").datepicker({dateFormat: 'dd-mm-yy'});
});
</script>
which picks up my input boxes with the mydatepickerclass assigned. and all works. Why would this stop working on the postback.
2) How do I fix this.... how do I wire it up so that after a postback in an updatepanel it still works.
I understand that the ID might change on a postback, I think but as I am using classes I don't know what is going wrong.
edit
I have the following code in my usercontrol where the update is happening:
<asp:UpdatePanel ID="HistoryUpdatePanel" runat="server">
<ContentTemplate>
<%-- Start of Company History section --%>
<fieldset>
<legend>Activity History</legend>
<script type="text/javascript">
$(function() {
$(".mydatepickerclass").datepicker({dateFormat: 'dd-mm-yy'});
});
</script>
<div>
<asp:ListBox ID="listBoxHistoryTypes" runat="server" SelectionMode="Multiple" AutoPostBack="true" OnSelectedIndexChanged="listBoxHistoryTypes_IndexChanged" />
<label>Date From:</label><asp:TextBox class="mydatepickerclass" ID="txtdatefrom" runat="server" />
<label>Date To:</label><input class="mydatepickerclass" type="text" />
<asp:TextBox class="mydatepickerclass" ID="txtdateto" runat="server" />
<asp:Button ID="btnFilterSearch" runat="server" Text="Filter Results" OnClick="btnFilterSearch_Click" />
</div>
</fieldset>
</ContentTemplate>
Does the script inside the updatepanel not rewire it?
Thanks
Jon Hawkins
Instead of doing this there is a simple alternative.
In the postback of the element in update panel add this code
And this in javascript
}
After Partial postback in updated panel it again call the datepicker function
You can do like this. in this case it will work always ;))
"jQuery UI Datepicker does not work after ajax partial postback"
Place a script manager and an update panel on the page.
Now place a text box and a button inside the updatepanel control.
Now bind the datepicker control with the text box.
Now create a server side click for the button which will cause an ajax partial postback.
On running the page, u would see something like this
Click on datepicker. The datepicker gets opened and selected date becomes value of the text box. Now click on button. The server side button click gets called and now you will see something like this.
The datepicker button is gone.So what do we do now to make it working within ajax. See below code.
pageLoad() function is available in JavaScript if you are using ASP.NET ajax. AJAX framework automatically wires up any client-side function named pageLoad() as an Application.Load handler
Source Link Credits
http://www.jquerybyexample.net/2010/08/jquery-datepicker-does-not-work-after.html
I know this post is old - but just place your jquery datepicker outside of the update panel - should work 100%...
the update panel is going to reload the contents of the html. You'll have to listen for the UpdatePanel to complete and recreate the datepicker.
Here is a very basic sample. This doesn't take into account multiple update panels on your page or potential memory leaks from not properly destroying your datepicker.
Another thing to note when mixing ASP.NET Ajax and jQuery be careful because the both use the $ in different contexts