I will explain the following code a bit and what you need to do.
At the first request from this page, the code checks if LocalTime has already been saved in the session, and if it will not write the form element, hidden input and javascript that will publish this form with local time. The response ends, so your report will not get the opportunity to be generated.
This submit will immediately create a POST request with localTime set, then ASP.Net will save that POST value in the session.
I also added a 302 redirect (Response.Redirect) to the original page due to usability. The user first made a GET request, not a POST, so if he wants to refresh the page, the browser will repeat the last action, which was that form.submit (), not a GET request.
You have local time. But you do not need to read it for each request, since it can be compared with UTC time, and then with a time server.
edit: you need to parse the UTC time in DateTime, but it is probably easy to find the format, although it may depend on the culture of the user (not sure about this statement).
public ReportPage() { this.Init += (o, e) => { // if the local time is not saved yet in Session and the request has not posted the localTime if (Session["localTime"] == null && String.IsNullOrEmpty(Request.Params["localTime"])) { // then clear the content and write some html, a javascript code which submits the local time Response.ClearContent(); Response.Write(@"<form id='local' method='post' name='local'> <input type='hidden' id='localTime' name='localTime' /> <script type='text/javascript'> document.getElementById('localTime').value = new Date(); document.getElementById('local').submit(); </script> </form>"); // Response.Flush(); // end the response so PageLoad, PagePreRender etc won't be executed Response.End(); } else { // if the request contains the localtime, then save it in Session if (Request.Params["localTime"] != null) { Session["localTime"] = Request.Params["localTime"]; // and redirect back to the original url Response.Redirect(Request.RawUrl); } } }; }
Adrian iftode
source share