Reputation: 165
I have a text box in HTML and we have text box onkeyup event.
So I want the latest UTC date-time from the server(C#) once we press any key in the textbox. For that, I have added JavaScript function on "keyUp" event of that text box like below-
<type="text" onkeyup="return SetUTCDateTime('@DateTime.UtcNow.ToString("yyyy-MM-ddTHH:mm:ss")'); />
<script type="text/javascript>
var currentUTCDateTime = null;
function SetUTCDateTime(currentMessageUTCDateTime)
{
currentUTCDateTime = currentMessageUTCDateTime;
}
</script>
I have taken the global variable in JavaScript to keep the time for further uses.
But my problem is when I enter any text into the textbox, the first time this function is giving correct time. But for any further/next clicks it's giving the same value but it should give the latest time.
FYI, I can't use client/browser time here because it may be invalid. That's why I want to use Server side DateTime value. And also I don't want to call any API to get latest UTC time from the server(due to performance issues).
Is there any way to get latest server-side time each time we click's on the textbox ?
Upvotes: 1
Views: 514
Reputation: 1
There is a problem with you perception of client-side and server-side and the way ASP.NET operates.
While you are trying to get the correct date/time from the server, you are never actually making a call to the server. The browser needs to contact the server to get the latest value of DateTime.UtcNow.
In your case, you are rendering the page with the value of DateTime.UtcNow as it was on the first HTTP request. Your HTML page is passed to the browser via HTTP, and the part below:
<type="text" onkeyup="return SetUTCDateTime('@DateTime.UtcNow.ToString("yyyy-MM-ddTHH:mm:ss")'); />
is converted to the following (assuming the call was made at 13:00 of the 1st of January):
<type="text" onkeyup="return SetUTCDateTime('2017-01-01T13:00:00');" />
So after you receive the HTML from the server, the time isn't variable anymore, but fixed to the value it had after being translated by ASP.NET. The next time you are triggering an event you are actually simply calling the javascript below, as that is what your page contains:
return SetUTCDateTime('2017-01-01T13:00:00');
And so your event will give the same value.
To do what you are trying to achieve, you have two options:
Create a service that yields the latest date/time and call the service via AJAX on the event you are implementing, or
(Not the best way to achieve this) post the page each time your event fires and it will automatically give the latest date with the code you already have.
But you need to make an HTTP request to the server if you need the latest date/time from the server.
Upvotes: 0
Reputation: 1774
It looks like you're trying to render the server's datetime into the SetUTCDateTime method, but that's only happening once when the page renders.
Assuming there's a good reason for wanting to get the time from the server, what you'd need to do is code the SetUTCDateTime method to do an ajax request back to the server, and have that action on the server return the current datetime.
Upvotes: 1