Click here to Skip to main content
15,892,674 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello all

Hope you are doing good.
I have a issue with datetime picker inside update panel.
When page load happens its working fine but after postback its not showing.
can anyone help me please ? below codes for reference.
thanks in advance.

JavaScript
$(function () {
    var currentDate = new Date();
    $("#txtStartDate, #txtEndDate").datetimepicker(
	{
	    timeFormat: "hh:mm tt",
	    hourMin: 8,
	    hourMax: 16,
	    numberOfMonths: 1,
	    minDate: 0,
	    maxDate: 30        
	});
    $("#txtStartDate,#txtEndDate").datepicker("setDate", currentDate);
});


Note : javascript in external file.



ASP.NET
<body>  
    <form id="form1" runat="server">
          <asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
        <asp:UpdatePanel ID="UpdatePanel1" runat="server">
            <ContentTemplate>
                <div style="width:80%; margin-removed15%;height:100%">
                    <table style="width: 90%; height:680px" class="table">
                        <tr>
                            <td style="height:30px"></td>
                            <td></td>
                            <td>
                                </td>
                            <td>
                                </td>
                        </tr>
                        <tr>
                            <td style="width:160px"></td>
                            <td style="width:240px">Category </td>
                            <td>
                                <asp:DropDownList ID="ddlCatName" runat="server" CssClass="dropdown" Width="350px" Height="35px" AutoPostBack="True" OnSelectedIndexChanged="ddlCatName_SelectedIndexChanged">
                                </asp:DropDownList>
                            </td>
                            <td>
                                <asp:RequiredFieldValidator ID="rfCategory" runat="server" ErrorMessage="Choose category" ForeColor="Red" ControlToValidate="ddlCatName" InitialValue="-   Select   -"></asp:RequiredFieldValidator>
                            </td>
                        </tr>
                        <tr>
                            <td></td>
                            <td>Sub-Category </td>
                            <td >
                                <asp:DropDownList ID="ddlSubCatName" runat="server" Width="350px" CssClass="dropdown" Height="35px" AutoPostBack="True" OnSelectedIndexChanged="ddlSubCatName_SelectedIndexChanged">
                                </asp:DropDownList>
                            </td>
                            <td>
                                <asp:RequiredFieldValidator ID="rfSubCat" runat="server" ErrorMessage="Choose sub-category" ForeColor="Red" ControlToValidate="ddlSubCatName" InitialValue="-   Select   -"></asp:RequiredFieldValidator>
                            </td>
                        </tr>
                         <tr>
                            <td></td>
                            <td>Start Date</td>
                            <td >
                                <asp:TextBox ID="txtStartDate" runat="server" Width="350px" CssClass="login_txtbox" Height="35px"></asp:TextBox>
                             </td>
                            <td>
                                <asp:RequiredFieldValidator ID="rfStartdate" runat="server" ErrorMessage="Enter start date" ForeColor="Red" ControlToValidate="txtStartDate"></asp:RequiredFieldValidator>
                             </td>
                        </tr>
                         <tr>
                            <td></td>
                            <td>End Date</td>
                            <td>
                                <asp:TextBox ID="txtEndDate" runat="server" Width="350px" CssClass="login_txtbox" Height="35px"></asp:TextBox>
                             </td>
                            <td>
                                <asp:RequiredFieldValidator ID="rfEndDate" runat="server" ErrorMessage="Enter end date" ForeColor="Red" ControlToValidate="txtEndDate"></asp:RequiredFieldValidator>
                             </td>
                        </tr>
                         <tr>
                            <td style="height:30px"></td>
                            <td></td>
                            <td>
                                <asp:Label ID="lblError" runat="server" ForeColor="Red"></asp:Label>
                             </td>
                            <td></td>
                        </tr>
                         <tr>
                            <td></td>
                            <td></td>
                            <td>
                                <asp:Button ID="btnSubmit" runat="server" Text="Submit" CssClass="buttons" Height="30px" Width="150px" OnClick="btnSubmit_Click" />
                             </td>
                            <td></td>
                        </tr>
                    </table>
                </div>
            </ContentTemplate>    
        </asp:UpdatePanel>
    </form>
</body>
Posted
Updated 5-Sep-16 2:54am
v3

1 solution

use pageLoad() function

JavaScript
function pageLoad() { 
      
            console.log('loaded');
            var currentDate = new Date();
            $("#txtStartDate, #txtEndDate").datetimepicker(
            {
                timeFormat: "hh:mm tt",
                hourMin: 8,
                hourMax: 16,
                numberOfMonths: 1,
                minDate: 0,
                maxDate: 30
            });
              $("#txtStartDate,#txtEndDate").datepicker("setDate", currentDate);
        };
 
Share this answer
 
Comments
dibyaaryan007 6-Sep-16 11:03am    
Thanks Karthik. You saved my time. Its working perfect now.
Thanks a lot again :)
Karthik_Mahalingam 6-Sep-16 11:19am    
welcome dibya :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900