locked
Datepicker showing today's date in the textbox RRS feed

  • Question

  • User219039814 posted

    I have the following datepicker

    $(function () {
    $('#<%=txtdob.ClientID %>').datepicker({
    dateFormat: 'dd/mm/yy',
    changeMonth: true,
    changeYear: true,
    yearRange: '1950:2100'
    });
    });

    <asp:TextBox ID="txtdob" runat="server" ClientIDMode="Static" CssClass="form-control"></asp:TextBox>

    When i run the page, the textbox shows today's date as 18-09-2018, because of which my insert query shows a runtime error.

    I do NOT want today's date in the text box. Can some one help me.

    I tried this piece of code in two different forms. One shows today's date and the other DOES not show today's date. I find this quite surprising as i copied the same lines into both the pages.

    Can some one help. I DONT want today's date to bound to textbox. How can i remove it.

    I have been breaking my head for one full hour. 

    Thank you

    Tuesday, September 18, 2018 11:20 AM

All replies

  • User475983607 posted

    What is the run-time error?

    The only way today's date get into a textbox is if you write code to put the date in the textbox or the user selected today's date.  It not something that magically happens.

    Try setting a breakpoint and debugging your code or posting sample code that reproduces the error.

    Tuesday, September 18, 2018 11:29 AM
  • User839733648 posted

    Hi vijaylakshmi,

    It seems that you've used the JqueryUI-Datapicker.

    I've tried your code on my side and it works well as image below.

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
        <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" />
        <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.8.0/css/bootstrap-datepicker.min.css" />
        <link rel="stylesheet" href="https://code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css" />
        <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
        <script type="text/javascript">
            $(function () {
                $('#<%=txtdob.ClientID %>').datepicker({
                    dateFormat: 'dd/mm/yy',
                    changeMonth: true,
                    changeYear: true,
                    yearRange: '1950:2100'
                });
            });
        </script>
    </head>
    <body>
        <form id="form1" runat="server">
            <asp:TextBox ID="txtdob" runat="server" ClientIDMode="Static" CssClass="form-control"></asp:TextBox>
        </form>
    </body>
    </html>

    I could not reproduce your problem. Maybe this is not your complete code.

    because of which my insert query shows a runtime error.

    If possible, please provide more details about your issue like the screenshot of error message or your running result.

    Besides, as mgebhard says, you could use F12 developer tools to debug your code.

    Best Regards,

    Jenifer 

    Wednesday, September 19, 2018 10:22 AM
  • User-1171043462 posted

    Your code is working for dd/MM/yyyy format

    <asp:TextBox ID="txtDate" runat="server" ReadOnly="true"></asp:TextBox>
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6/jquery.min.js" type="text/javascript"></script>
        <script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.8/jquery-ui.min.js"
            type="text/javascript"></script>
        <link href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.8/themes/base/jquery-ui.css"
            rel="Stylesheet" type="text/css" />
        <script type="text/javascript">
            $(function () {
                $("[id*=txtDate]").datepicker({
                    dateFormat: 'dd/mm/yy',
                    changeMonth: true,
                    changeYear: true,
                    yearRange: '1950:2100'
                });
            });
        </script>

    Wednesday, September 19, 2018 2:58 PM
  • User61956409 posted

    Hi vijaylakshmi,

    When i run the page, the textbox shows today's date as 18-09-2018

    I tried this piece of code in two different forms. One shows today's date and the other DOES not show today's date.

    Please check if some code for setting date for your textbox are written on JavaScript client or codebehind. 

    Besides, you can try to explicitly set null value for your textbox, like below:

    In JavaScript:

     $("#<%=txtdob.ClientID %>").val("");

    In Codebehind:

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            //your code logic here
    
            txtdob.Text = "";
        }
               
    }

    With Regards,

    Fei Han

    Monday, September 24, 2018 8:52 AM