cant update label using timer - asp.net

i have a master page which uses script manager and on another page i want to show a digital clock which counts from 0 to 10 minutes.
asp.net code:
<asp:ScriptManagerProxy ID="ScriptManagerProxy1" runat="server">
</asp:ScriptManagerProxy>
<asp:Timer ID="Timer1" runat="server" Interval="5000" ontick="Timer1_Tick">
</asp:Timer>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<Triggers>
<asp:AsyncPostBackTrigger ControlID="Timer1" EventName="Tick" />
</Triggers>
<ContentTemplate>
<asp:Label ID="Label1" runat="server" Text=""></asp:Label>
</ContentTemplate>
</asp:UpdatePanel>
C# code:
int min=0,sec=0;
protected void Timer1_Tick(object sender, EventArgs e)
{
if (sec == 55)
{
min = min + 1;
sec = 0;
}
else
{
sec = sec + 5;
}
Label1.Text = min.ToString() +":"+ sec.ToString();
if(min==10&&sec==0)
{
Timer1.Enabled=false;
Label1.Text = "clock over!";
}
the page starts with 0:5 and doesn't change at all. pls point out if there is an error in d code!

My bet is that it will be your update panel. For debug purposes
Try without the upadte panel at all - does it work then
Put the timer in the update panel and forget about the trigger
In fact point 2 could be done anyway - I'm not sure of the benefit of having it as a postback trigger. It would do just as well with the timer inside
However
Honestly I wouldn't do this. Won't the Tick method perform a postback (albeit partial) just to get the timer display going.
There are lots of freely available components that will run purely on the client side which will be a lot (lot) more efficient e.g.
JQuery Timer
EDIT
Actually I think I've spotted the problem. It is how you record mins and secs. These are in variables and would be reset every postback (Timer tick). Youu would need to store them into something that would perist postback e.g. ViewState
protected void Timer1_Tick(object sender, EventArgs e) {
ViewState["min"] //not int min
ViewState["sec"] //not int sec
//.. you would need to use Convert (or Int32.Parse) to get
//.. then as an int to work with
}
But homestly - still don't do it. Use the JQuery component or similar

Related

private static variable is replace with latest value when accessing from different browser - c#

My web page is showing the details of selected student(using the studentid) in a gridview. I have put an updatepanel with timer for this gridview to auto refresh the data within an interval of time. But now the issue is when a user1 is seeing the details of student1 from his system. and another user2 is seeing the details of student2 from his system. when the timer executes the updatepanel , both the user is seeing the latest student (student2) details.
In short, when same web page is used by multiple users at same time, it shows the student details of latest user selected.
I have set the student id as private static. Will it be individual for each browser? Or will it be the issue of updatepanel with timer?
<asp:UpdatePanel runat="server" ID="UpdatePanel1" UpdateMode="Conditional">
<ContentTemplate>
<asp:Timer ID="Timer2" runat="server" Interval= "<%$appSettings:update_timer%>" OnTick="Timer1_Tick"></asp:Timer>
<asp:GridView ID="gv_studentdetails" runat="server" AutoGenerateColumns="true" Caption="Student Details"
CssClass="gridview_alter" >
</asp:GridView>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="Timer2" EventName="Tick" />
</Triggers>
</asp:UpdatePanel>
In web.config file
<add key="update_timer" value="30000"/>
Code behind:
private static int student_id;
if (!IsPostBack)
{
student_id=Convert.ToInt32(Session["selected_studentID"]);
BindGridview(student_id);
}
protected async void Timer1_Tick(object sender, EventArgs e)
{
BindGridview(student_id);
UpdatePanel1A.Update();
}
NB: This issue is happening when run the timer of updatepanel only. When manually reload the page it is not .
private static int student_id;
The value will be the same within a single web-server. No matter the browser sessions. That is why both browsers start to see the last value for the server.
But whenever someone reloads the page it will set the private field to their won session's selected_studentID. So then all the browsers will get that value evenutally.
if (!IsPostBack)
{
student_id=Convert.ToInt32(Session["selected_studentID"]);
BindGridview(student_id);
}
Note:
Manipulating a static instance without lock, may cause an issue. You can read more here c# critical sections, lock

showing the loop value in label runtime ASP..NET

Iam newbie to ASP.NET, and this is my first question on this forum.
Here is my code:
protected void Button1_Click(object sender, EventArgs e)
{
for (int i = 0; i < 10; i++)
{
Label2.Text = i.ToString();
UpdatePanel1.Update();
Thread.Sleep(3000);
}
}
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:Label ID="Label1" runat="server" Text=""></asp:Label>
<asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click"/>
<asp:Label ID="Label2" runat="server" Text=""></asp:Label>
</ContentTemplate>
</asp:UpdatePanel>
I want to display the value of i each time when its get updated, but I got the value 9.
Please help me how can achieve my goal... No luck so far
You can't update the page in the browser directly from server code. Any updates to the page are sent back to the page when the server code ends.
The Update method only tells the update panel that it should be included in the data that is sent back to the browser when the page is complete, it doesn't send the update directly to the browser.
To periodically update content in the browser using server code you have to control it from the browser, and the server code should just do one update and then exit so that the update is sent back to the browser. You can user a timer control that will make requests to the server for every tick, or you could use Javascript code to request data from a server page.

TextBox loses its focus when timer is ticking

I put a timer control on a specific section on my page, but every time the timer is ticking, my text box (I have multiple text boxes) in another section loses it focus.
How can I resolve this? I tried placing the timer in a separate update panel.
the code in timer Tick event is
protected void Timer1_Tick(object sender, EventArgs e)
{
if ((List<AllPostInformation>)Session["AllNewsPostCollection"] != (List<AllPostInformation>)Session["CheckExistData"])
{
if ((List<AllPostInformation>)Session["AllNewsPostCollection"] != null)
{
List<AllPostInformation> o = new List<AllPostInformation>();
o = (List<AllPostInformation>)Session["AllNewsPostCollection"];
rptNews.DataSource = o;
rptNews.DataBind();
}
Session["CheckExistData"] = Session["AllNewsPostCollection"];
}
}
and on asp page
<asp:UpdatePanel runat="server" ID="upTimer">
<Triggers>
<asp:AsyncPostBackTrigger ControlID="Timer1" EventName="Tick" />
</Triggers>
<ContentTemplate>
<asp:Timer ID="Timer1" runat="server" Interval="5000" />
</ContentTemplate>
</asp:UpdatePanel>
If you look at the post in this link, Focus lost on partial postback with UserControls inside UpdatePanel you will get idea that you need not to put the timer control in the update panel instead put the items that are going to be updated.

parralel ASP.NET Timer Controls

I have a problem and I'd really appreciate a little help.
On my .aspx I have four ASP.NET Timer Controls and when each of them Ticks I call asynchronously different web service method that returns me some photos. I have a image in Update Panel and update it src when I get a photo. I'm using a jQuery lightBox plugin to preview a image.
The problem is that: I would like the Timers work in the same time (simultaneously). So on page Load, after some seconds should appear a Tick of each Timers (parallel, more less in the same time). Both Timers have the same time Interval so it should work, but in practice I've noticed that the second Timer ticks only after the first web service callback appears, and the third Timer ticks after the second web service callback appears. So it all works linearly, not parallel.
Any solutions how to modify it to Tick simultaneously ?
my .aspx:
<center>
<asp:Timer runat="server" ID="UpdateTimer1" OnTick="UpdateTimer1_Tick" />
<asp:UpdatePanel runat="server" ID="TimedPanel2" UpdateMode="Conditional">
<Triggers>
<asp:AsyncPostBackTrigger ControlID="UpdateTimer1" EventName="Tick" />
</Triggers>
<ContentTemplate>
<asp:Literal runat="server" ID="Ltr1" />
<div id="div1" runat="server">
<img alt="" id="img1" runat="server" style="width: 85%; height: 85%" />
</div>
</ContentTemplate>
</asp:UpdatePanel>
</center>
one of my Timers in .aspx.cs, the rest of Timers is similar - the call different webservice, but have the same events, updateTimer_Ticks etc:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
UpdateTimer1.Interval = 3000;
{
}
protected void UpdateTimer1_Tick(object sender, EventArgs e)
{
AsynchGetPhoto();
}
public void AsynchGetPhoto()
{
WS.Service Service = new WS.Service();
Service.getPhotoCompleted += new Service.getPhotoCompletedEventHandler(PhotoCompleted);
Service.getPhotoAsync();
}
public void PhotoCompleted(object sender, WS.Service.getPhotoCompletedEventArgs args)
{
img1.Attributes.Add("src", "data:image/jpg;base64," + args.Result);
}
What i can tell you about your problem is my understanding about how this timer is working and happens behind the scene and you can investigate more in your code.
When the timers tick an asynchronous request to your function is done so each timer shall fire it's own request and the browser shall handle all the requests in parallel so i suggest that you open the firebug and see your timers requests and see if they are depending on each other and each request is handled after the other or it is just the rendering mechanism of the browser causing the timers to delay.

UpdatePanel wrapped around a user control

I have a user control which contains some buttons and a placeholder. Those buttons cause controls to be added/removed from placeholder. Everything works fine.
Now I want to put this user control in a page, and wrap it in an updatepanel like so:
<asp:UpdatePanel ChildrenAsTriggers="true" ID="UpdatePanelFoo" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<grid:tablegrid ID="tablegrid_chapters" runat="server" SomeProperty="bar" />
</ContentTemplate>
</asp:UpdatePanel>
When I run the page, it's still doing a full postback when I hit one of the buttons inside the user control. What am I doing wrong, and how can I remedy this?
Update:
protected void Page_Init()
{
ScriptManager scr = ScriptManager.GetCurrent(this.Page);
Response.Write("EnablePartialRendering: " + scr.EnablePartialRendering);
}
Outputs "EnablePartialRendering: true"
Make sure you have EnablePartialRendering=true on your ScriptManager in the page.
Update
It looks like your UserControl has no events to be looking for...you have 2 options here. Move the UpdatePanel inside the UserControl .ascx so it can see the button events as children to rig up or add an event for it to see, to do that try something like this:
public event EventHandler Click;
void btn_del_Click(object sender, EventArgs e)
{
if (NumberOfRowControls > 0)
{
var rowToWhack = panel_rows.Controls.Children().Single(x => x.ID == "myrow" + (NumberOfRowControls - 1));
panel_rows.Controls.Remove(rowToWhack);
NumberOfRowControls--;
}
if(Click != null) Click(this, e);
}
void btn_add_Click(object sender, EventArgs e)
{
var row = NewRow(NumberOfRowControls);
panel_rows.Controls.Add(row);
if(Click != null) Click(this, e);
}
And update the UpdatePanel to be looking for it:
<asp:UpdatePanel ID="UpdatePanelFoo" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<grid:tablegrid ID="tablegrid_chapters" runat="server" SomeProperty="bar" />
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="tablegrid_chapters" EventName="Click">
</Triggers>
</asp:UpdatePanel>
Make sure you add a ScriptManager as well to the page, otherwise there's no UpdatePanel functionality.

Resources