Answered by:
Label value does not change with ajax timer

Question
-
User-418973555 posted
having an ajax timer running to check connection,
wondering why the label lblsessiontimeleft inside update panel udpSession does not change accordingly, meant for it to decrease its value by one second thereafter
protected void TimerSession_Tick(object sender, EventArgs e) { recheckconn(); if ((string)Session["sConnLetter"] == "c") { DateTime datetimenow = DateTime.Now; DateTime datetimesession; datetimesession = datetimenow.AddMilliseconds((int)Session["sSessionTimeout"]); TimeSpan tstd; TimeSpan tstd1; tstd = datetimesession.AddMilliseconds((int)Session["sSessionTimeout"]) - DateTime.Now; tstd1 = tstd.Subtract(new TimeSpan(0, 0, 1)); if (Session["sSessionTimeout1"] != null) { Session["sTimeDifferent"] = datetimesession.AddMilliseconds((int)Session["sSessionTimeout1"]) - DateTime.Now; tstd = datetimesession.AddMilliseconds((int)Session["sSessionTimeout1"]) - DateTime.Now; tstd1 = tstd.Subtract(new TimeSpan(0, 0, 1)); } Session["sSessionTimeout1"] = tstd1.Milliseconds; lblsessiontimeleft.Text = tstd1.Milliseconds.ToString(); pnlSession.Style.Add("display", "block"); udpSession.Update(); }
Friday, May 10, 2019 3:00 PM
Answers
-
User839733648 posted
Hi
According to your code, I've made a test on my side.
Since I'm not clear about the session's value. I've customized it.
And I do reprodcue your issue, the label's text will always the value without changing,
I've made debug and find that the request is sent every second.
But when I add the Thread.Sleep() method to affect DateTime.Now, the value will change.
The reason for the value not change may be the logic of your code.
Your calculation is about the datetime, the logic may result in the vlaue not changing. Please check that.
Here is my testing code.
<asp:ScriptManager ID="ScriptManager1" runat="server" ></asp:ScriptManager> <asp:Timer ID="Timer1" runat="server" OnTick="Timer1_Tick" Interval="1000"></asp:Timer> <asp:UpdatePanel ID="UpdatePanel1" runat="server"> <ContentTemplate> <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label> </ContentTemplate> <Triggers> <asp:AsyncPostBackTrigger ControlID="Timer1"/> </Triggers> </asp:UpdatePanel>
protected void Timer1_Tick(object sender, EventArgs e) { Session["sSessionTimeout"] = 1; Session["sSessionTimeout1"] = 2; DateTime datetimenow = DateTime.Now; DateTime datetimesession; datetimesession = datetimenow.AddMilliseconds((int)Session["sSessionTimeout"]); Thread.Sleep(new Random().Next(3000)); TimeSpan tstd; TimeSpan tstd1; tstd = datetimesession.AddMilliseconds((int)Session["sSessionTimeout"]) - DateTime.Now; tstd1 = tstd.Subtract(new TimeSpan(0, 0, 1)); if (Session["sSessionTimeout1"] != null) { Session["sTimeDifferent"] = datetimesession.AddMilliseconds((int)Session["sSessionTimeout1"]) - DateTime.Now; tstd = datetimesession.AddMilliseconds((int)Session["sSessionTimeout1"]) - DateTime.Now; tstd1 = tstd.Subtract(new TimeSpan(0, 0, 1)); } Session["sSessionTimeout1"] = tstd1.Milliseconds; var str = tstd1.Milliseconds.ToString(); Label1.Text = tstd1.Milliseconds.ToString(); }
Best Regards,
Jenifer
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Monday, May 13, 2019 8:28 AM
All replies
-
User839733648 posted
Hi
According to your code, I've made a test on my side.
Since I'm not clear about the session's value. I've customized it.
And I do reprodcue your issue, the label's text will always the value without changing,
I've made debug and find that the request is sent every second.
But when I add the Thread.Sleep() method to affect DateTime.Now, the value will change.
The reason for the value not change may be the logic of your code.
Your calculation is about the datetime, the logic may result in the vlaue not changing. Please check that.
Here is my testing code.
<asp:ScriptManager ID="ScriptManager1" runat="server" ></asp:ScriptManager> <asp:Timer ID="Timer1" runat="server" OnTick="Timer1_Tick" Interval="1000"></asp:Timer> <asp:UpdatePanel ID="UpdatePanel1" runat="server"> <ContentTemplate> <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label> </ContentTemplate> <Triggers> <asp:AsyncPostBackTrigger ControlID="Timer1"/> </Triggers> </asp:UpdatePanel>
protected void Timer1_Tick(object sender, EventArgs e) { Session["sSessionTimeout"] = 1; Session["sSessionTimeout1"] = 2; DateTime datetimenow = DateTime.Now; DateTime datetimesession; datetimesession = datetimenow.AddMilliseconds((int)Session["sSessionTimeout"]); Thread.Sleep(new Random().Next(3000)); TimeSpan tstd; TimeSpan tstd1; tstd = datetimesession.AddMilliseconds((int)Session["sSessionTimeout"]) - DateTime.Now; tstd1 = tstd.Subtract(new TimeSpan(0, 0, 1)); if (Session["sSessionTimeout1"] != null) { Session["sTimeDifferent"] = datetimesession.AddMilliseconds((int)Session["sSessionTimeout1"]) - DateTime.Now; tstd = datetimesession.AddMilliseconds((int)Session["sSessionTimeout1"]) - DateTime.Now; tstd1 = tstd.Subtract(new TimeSpan(0, 0, 1)); } Session["sSessionTimeout1"] = tstd1.Milliseconds; var str = tstd1.Milliseconds.ToString(); Label1.Text = tstd1.Milliseconds.ToString(); }
Best Regards,
Jenifer
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Monday, May 13, 2019 8:28 AM -
User-418973555 posted
made some modifications,
protected void Page_Load(object sender, EventArgs e) { Response.Cache.SetCacheability(HttpCacheability.NoCache); if (!IsPostBack) { lblErrMsg.Text = String.Empty; txtProductName.Text = String.Empty; txtProductDetail.Text = String.Empty; Session["Reset"] = true; Configuration config = WebConfigurationManager.OpenWebConfiguration("~/Web.Config"); SessionStateSection section = (SessionStateSection)config.GetSection("system.web/sessionState"); // timeout in miliseconds double timeout = (double)section.Timeout.TotalMinutes * 1000 * 60; //ClientScript.RegisterStartupScript(this.GetType(), "SessionAlert", "SessionExpireAlert(" + timeout + ");", true); Session["sSessionTimeout"] = timeout; if ((bool)Session["Reset"] == true) { Session["sSessionTimeout"] = timeout; Session["sSessionTimeout1"] = null; } } } ... protected void TimerSession_Tick(object sender, EventArgs e) { if ((string)Session["sConnLetter"] == "c") { DateTime datetimenow = DateTime.Now; TimeSpan tstd; TimeSpan tstd1; if (Session["sSessionTimeout1"] != null) { Session["sTimeDifferent"] = datetimenow.AddMilliseconds((double)Session["sSessionTimeout1"]) - DateTime.Now; tstd = datetimenow.AddMilliseconds((double)Session["sSessionTimeout1"]) - DateTime.Now; tstd1 = tstd.Subtract(new TimeSpan(0, 0, 1)); Session["sSessionTimeout1"] = tstd1.TotalMilliseconds; } else { tstd = datetimenow.AddMilliseconds((double)Session["sSessionTimeout"]) - DateTime.Now; tstd1 = tstd.Subtract(new TimeSpan(0, 0, 1)); Session["sSessionTimeout1"] = tstd1.TotalMilliseconds; } lblsessiontimeleft.Text = tstd1.Hours.ToString() + ":" + tstd1.Minutes.ToString() + ":" + tstd1.Seconds.ToString(); pnlSession.Style.Add("display", "block"); udpSession.Update(); } if ((string)Session["sConnLetter"] == "n") { pnlSession.Style.Add("display", "none"); udpSession.Update(); } }
Tuesday, May 14, 2019 3:28 AM -
User839733648 posted
Hi larnvok09,
According to your code, I'm not clear about your issue.
There occurs some error message when I try the code because of code lost.
If possible, please provide more details about your issue like the complete code including the front code and behind code,
also hope that you could describe your issue about your modified codes, this will be easier for us to help with you.
Best Regards,
Jenifer
Tuesday, May 14, 2019 7:30 AM