Reputation: 193
I have a timer with an interval of 1000 (1 second). The timer is executing a SQL query and put the result which is a number in a label.text.
The problem occurs when I Scroll the page up and down. I get lags and stuck while scrolling. If I change the interval to 10 (just out of curiosity) the lags is huge ! Your help is appreciated.
Here is my HTML Script:
<div>
<asp:Timer ID="Timer1" runat="server" Interval="10000" ontick="Timer1_Tick">
</asp:Timer>
</div>
<asp:UpdatePanel ID="UpdatePanel5" runat="server" UpdateMode="Always">
<triggers>
<asp:AsyncPostBackTrigger ControlID="Timer1" EventName="Tick" />
</triggers>
<contenttemplate>
<asp:Label ID="Label1" class="button2" runat="server"></asp:Label>
</contenttemplate>
</asp:UpdatePanel>
CS:
protected void Timer1_Tick(object sender, EventArgs e)
{
con.Open();
cmd = new SqlCommand("exec time_proc1", con);
dr = cmd.ExecuteReader();
while (dr.Read())
{
Label1.Text = dr["seconds"].ToString();
}
Upvotes: 0
Views: 3040
Reputation: 193
So i just done it with an iframe, the timer code is in a different page, no problem now.
Upvotes: 0
Reputation: 13013
As it seems right now, your code doing its job synchronously (POST) every 10 seconds (10,000 miliseconds)!
You should eliminates the need to refresh the whole page with each postback, I would suggest you to convert your code to work asynchronously in order to avoid the very bad scrolling experience.
Please take a look at this nice tutorial from MSDN: How to refresh an UpdatePanel control at a timed interval
And to facilitate:
Code-Behind
protected void Timer1_Tick(object sender, EventArgs e)
{
Label1.Text = "Panel refreshed at: " + DateTime.Now.ToLongTimeString();
}
ASP.NET
<form id="form1" runat="server">
<asp:ScriptManager runat="server" id="ScriptManager1">
</asp:ScriptManager>
<asp:UpdatePanel runat="server" id="UpdatePanel1">
<ContentTemplate>
<asp:Timer runat="server" id="Timer1" Interval="10000" OnTick="Timer1_Tick"> </asp:Timer>
<asp:Label runat="server" Text="Page not refreshed yet." id="Label1">
</asp:Label>
</ContentTemplate>
</asp:UpdatePanel>
<asp:Label runat="server" Text="Label" id="Label2"></asp:Label>
</form>
Upvotes: 1
Reputation: 134105
Whenever that timer tick happens, it posts back to the Web server. I suspect it's waiting for the server to respond before continuing. So if you have that running every second, then every second you're doing a postback and waiting for the server to respond. That's definitely going to cause some non-smooth scrolling.
When you changed the timer interval to 10, that made the timer fire every 10 milliseconds, or 100 times per second. It probably doesn't actually do 100 postbacks per second (I would hope not), but it'd almost certainly making a new request immediately after getting the response from the previous one. So there won't be a lot of time for the browser to get your input and scroll.
The problem you're experiencing is one reason AJAX became popular. It makes asynchronous requests that largely don't interfere with the UI responsiveness. If you want that periodic update and smooth UI operation, you'll have to look into making that timer (or something similar) do asynchronous requests.
Unfortunately, I'm not the guy to show you how to do that. My experience with ASP.NET controls is ... dated.
Upvotes: 1