How to get the value of DateTimePicker in the ASP.Net application?

piglet 1 Reputation point
2022-06-14T00:15:14+00:00

I have an asp.net application in which master page is used as well as the DateTimePicker JS function.

<div class="col-sm-6" >
<input type="text" id="datetimepicker" />
</div>

  <script type="text/javascript" >  
      $("#datetimepicker").datetimepicker({  
          format: 'YYYY-MM-DD HH:mm',  
          locale: moment.locale('zh-CN'),  
      });  

</script>

The idea is the Date information showed in the input control can be obtained and can be used as a parameter in the UploadShift function.

protected void btnUpload_Click(object sender, EventArgs e)
{
string dateTime = Request.Form["datetimepicker"];

        DateTime endDate = Convert.ToDateTime(dateTime);             
        int result = shiftController.UploadShift(endDate);  
    }  

The problem is the datetime information can not be retrieved every time. Please help me fix the issue.THX!!!

ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,244 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. piglet 1 Reputation point
    2022-06-14T01:18:26.267+00:00

    Thx. I found a solution to work out.

    0 comments No comments

  2. Yijing Sun-MSFT 7,061 Reputation points
    2022-06-14T02:30:36.517+00:00

    Hi @piglet ,

    The problem is the datetime information can not be retrieved every time.

    What means can not be retrieved every time? Do you get the value occasionally, but not every so often? As far as I think, the code behind can't get the value from the js datetimepicker. I suggest you could get the value using js and then use ajax to post to the code behind.

    js like this:
    211055-screenshot-2022-06-14-102740.png

    code-behind:

    [WebMethod]  
            public static string Test(string tb)  
            {  
                DateTime endDate = Convert.ToDateTime(tb);  
                int result = shiftController.UploadShift(endDate);  
                return result.ToString();  
            }  
    

    Best regards,
    Yijing Sun


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments