question

piglet-6010 avatar image
0 Votes"
piglet-6010 asked YijingSun-MSFT answered

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

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!!!

dotnet-aspnet-webpages
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

piglet-6010 avatar image
0 Votes"
piglet-6010 answered

Thx. I found a solution to work out.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

YijingSun-MSFT avatar image
0 Votes"
YijingSun-MSFT answered

Hi @piglet-6010 ,

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.


5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.