Retain (Preserve) Selected date of jQuery DatePicker after PostBack (Button Click) in ASP.Net

Last Reply on Nov 09, 2015 03:52 AM By Shashikant

Posted on Nov 09, 2015 03:02 AM

hi ;

I have a problem about jquery datetimepicker,I have assigned default date value page load and When I change to it and click button the date value change to defalt after buton code executed

So ; I assign 01.11.2015 as default and change to it 01.07.2015 and click button then the date value changed to 01.11.2015 ,my code like bellow .

$(document).ready(function () {
    var date = new Date();
    var today = new Date();
    var firstOfMonth = new Date(today.getFullYear(), today.getMonth(), 1);
    date.setMonth(date.getMonth() + 1, 1);
    $("#Bastar").datepicker({
        defaultDate: firstOfMonth,
        showOn: "button",
        buttonImage: "/Images/calendar.ico",
        buttonImageOnly: true
    });
    $('#Bastar').datepicker('setDate', firstOfMonth);
});

Thanks in advance .

Posted on Nov 09, 2015 03:52 AM Modified on on Nov 09, 2015 04:11 AM

Please refer below code.

HTML

<div>
    <link rel="stylesheet" href="http://code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">
    <script src="http://code.jquery.com/jquery-1.10.2.js"></script>
    <script src="http://code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
    <link rel="stylesheet" href="/resources/demos/style.css">
    <script type="text/javascript">
        $(document).ready(function () {
            var date = new Date();
            var today = new Date();
            var firstOfMonth = new Date(today.getFullYear(), today.getMonth(), 1);
            date.setMonth(date.getMonth() + 1, 1);
            $("#txtDate").datepicker({
                defaultDate: firstOfMonth,
                showOn: "button",
                buttonImage: "/Images/calendar.ico",
                buttonImageOnly: true
            });

            if ('<%=this.IsPostBack%>'.toLowerCase() == 'false') {
                $('#txtDate').datepicker('setDate', firstOfMonth);
            }
        });
    </script>
    <div>
        <asp:TextBox ID="txtDate" runat="server" />
    </div>
    <br />
    <br />
    <asp:Button Text="Refresh" runat="server" />
</div>

Screenshot