简体   繁体   中英

Unable to capture datetime value from jquery datetime picker in asp.net page (C#)

Pretty simple, here is the js:

 <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.6/jquery.min.js" type="text/javascript"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/jquery-ui.min.js" type="text/javascript"></script>
<link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/themes/base/jquery-ui.css" rel="Stylesheet" type="text/css" />
<script type="text/javascript">
    $(function () {
        $("[id$=txt_Date]").datepicker({      
        });

    });

    //Set the default date today
    $(document).ready(function () {
        var currentDate = new Date();
        $("#txt_Date").datepicker("setDate", currentDate);
    });

</script>

Here is the control on the page. The page has a master page and in the content area the control is nested in an asp:Table, asp:TableRow, asp:TableCell structure.

<asp:TextBox ID="txt_Date" columns="30" runat="server" ReadOnly="true" name="RequestDate"></asp:TextBox>

Here is the code behind where I attempt to capture the value on a submit_click:

Date = Convert.ToDateTime(Request.Form["txt_Date"]);

I have also tried:

Date = Convert.ToDateTime(Request.Form["RequestDate"]);

Every attempt has returned 01/01/0001 as the date which I am guessing is just the default when it reads a NULL value from the control I am sure this is probably simple but I must be missing it.

you can simply use:

Date = Convert.ToDateTime(txt_Date.Text);

I have noticed one more thing which is not required

 $(function () {
        $("[id$=txt_Date]").datepicker({      
        });    
    });

As you are already writing initialization & assignment in document ready event.

Because ReadOnly is set to true, Request.Form will not contain your data. You can either access the Text property as suggested in another answer, or you can remove the ReadOnly attribute, which I think is the better option. It's generally best to handle user input through validation instead of attempting to restrict said input (I remember the days when everyone thought it was a good idea to disable the back button).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM