Timer Tick not increasing values on time interval

Clash Royale CLAN TAG#URR8PPP
up vote
6
down vote
favorite
I want to increase values on timer tick event but it is not increasing dont know what i am forgetting it is showing only 1.
<asp:Timer ID="Timer1" runat="server" OnTick="Timer1_Tick" Interval="1000">
</asp:Timer>
private int i = 0;
protected void Timer1_Tick(object sender, EventArgs e)
i++;
Label3.Text = i.ToString();
c# asp.net
add a comment |Â
up vote
6
down vote
favorite
I want to increase values on timer tick event but it is not increasing dont know what i am forgetting it is showing only 1.
<asp:Timer ID="Timer1" runat="server" OnTick="Timer1_Tick" Interval="1000">
</asp:Timer>
private int i = 0;
protected void Timer1_Tick(object sender, EventArgs e)
i++;
Label3.Text = i.ToString();
c# asp.net
For that use-case, you really need to use JavaScript (I guess your actual code is more complex though and needs something server-side)
â Jonathan Twite
1 hour ago
add a comment |Â
up vote
6
down vote
favorite
up vote
6
down vote
favorite
I want to increase values on timer tick event but it is not increasing dont know what i am forgetting it is showing only 1.
<asp:Timer ID="Timer1" runat="server" OnTick="Timer1_Tick" Interval="1000">
</asp:Timer>
private int i = 0;
protected void Timer1_Tick(object sender, EventArgs e)
i++;
Label3.Text = i.ToString();
c# asp.net
I want to increase values on timer tick event but it is not increasing dont know what i am forgetting it is showing only 1.
<asp:Timer ID="Timer1" runat="server" OnTick="Timer1_Tick" Interval="1000">
</asp:Timer>
private int i = 0;
protected void Timer1_Tick(object sender, EventArgs e)
i++;
Label3.Text = i.ToString();
c# asp.net
c# asp.net
edited 1 hour ago
Damien_The_Unbeliever
189k17241328
189k17241328
asked 1 hour ago
Jitender
816
816
For that use-case, you really need to use JavaScript (I guess your actual code is more complex though and needs something server-side)
â Jonathan Twite
1 hour ago
add a comment |Â
For that use-case, you really need to use JavaScript (I guess your actual code is more complex though and needs something server-side)
â Jonathan Twite
1 hour ago
For that use-case, you really need to use JavaScript (I guess your actual code is more complex though and needs something server-side)
â Jonathan Twite
1 hour ago
For that use-case, you really need to use JavaScript (I guess your actual code is more complex though and needs something server-side)
â Jonathan Twite
1 hour ago
add a comment |Â
3 Answers
3
active
oldest
votes
up vote
6
down vote
accepted
You can use ViewState to store and then read the value of i again.
int i = 0;
protected void Timer1_Tick(object sender, EventArgs e)
//check if the viewstate with the value exists
if (ViewState["timerValue"] != null)
//cast the viewstate back to an int
i = (int)ViewState["timerValue"];
i++;
Label3.Text = i.ToString();
//store the value in the viewstate
ViewState["timerValue"] = i;
This is working...Thanks Buddy.
â Jitender
1 hour ago
1
You're welcome. And if you want to save the value ofiacross multiple pages or still having the value after pressing F5 or reloading the page, useSession.
â VDWWD
1 hour ago
add a comment |Â
up vote
1
down vote
Check whether the form is posted back and then assign values. Check IsPostBack
private int i;
protected void Timer1_Tick(object sender, EventArgs e)
if (!IsPostBack)
i = 0;
else
i = Int32.Parse(Label3.Text);
i++;
Label3.Text = i.ToString();
this is not working every time it is showing 1
â Jitender
1 hour ago
check my updated code
â mbharanidharan88
1 hour ago
add a comment |Â
up vote
1
down vote
Generally speaking it is not a good practice to store values inside of views (such as asp.net page). It could be overwritten each time the request is sent.
You could store your data elsewhere:
public static class StaticDataStorage
public static int Counter = 0;
And use it:
protected void Timer1_Tick(object sender, EventArgs e)
StaticDataStorage.Counter++;
Label3.Text = StaticDataStorage.Counter.ToString();
add a comment |Â
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
6
down vote
accepted
You can use ViewState to store and then read the value of i again.
int i = 0;
protected void Timer1_Tick(object sender, EventArgs e)
//check if the viewstate with the value exists
if (ViewState["timerValue"] != null)
//cast the viewstate back to an int
i = (int)ViewState["timerValue"];
i++;
Label3.Text = i.ToString();
//store the value in the viewstate
ViewState["timerValue"] = i;
This is working...Thanks Buddy.
â Jitender
1 hour ago
1
You're welcome. And if you want to save the value ofiacross multiple pages or still having the value after pressing F5 or reloading the page, useSession.
â VDWWD
1 hour ago
add a comment |Â
up vote
6
down vote
accepted
You can use ViewState to store and then read the value of i again.
int i = 0;
protected void Timer1_Tick(object sender, EventArgs e)
//check if the viewstate with the value exists
if (ViewState["timerValue"] != null)
//cast the viewstate back to an int
i = (int)ViewState["timerValue"];
i++;
Label3.Text = i.ToString();
//store the value in the viewstate
ViewState["timerValue"] = i;
This is working...Thanks Buddy.
â Jitender
1 hour ago
1
You're welcome. And if you want to save the value ofiacross multiple pages or still having the value after pressing F5 or reloading the page, useSession.
â VDWWD
1 hour ago
add a comment |Â
up vote
6
down vote
accepted
up vote
6
down vote
accepted
You can use ViewState to store and then read the value of i again.
int i = 0;
protected void Timer1_Tick(object sender, EventArgs e)
//check if the viewstate with the value exists
if (ViewState["timerValue"] != null)
//cast the viewstate back to an int
i = (int)ViewState["timerValue"];
i++;
Label3.Text = i.ToString();
//store the value in the viewstate
ViewState["timerValue"] = i;
You can use ViewState to store and then read the value of i again.
int i = 0;
protected void Timer1_Tick(object sender, EventArgs e)
//check if the viewstate with the value exists
if (ViewState["timerValue"] != null)
//cast the viewstate back to an int
i = (int)ViewState["timerValue"];
i++;
Label3.Text = i.ToString();
//store the value in the viewstate
ViewState["timerValue"] = i;
answered 1 hour ago
VDWWD
21.9k113250
21.9k113250
This is working...Thanks Buddy.
â Jitender
1 hour ago
1
You're welcome. And if you want to save the value ofiacross multiple pages or still having the value after pressing F5 or reloading the page, useSession.
â VDWWD
1 hour ago
add a comment |Â
This is working...Thanks Buddy.
â Jitender
1 hour ago
1
You're welcome. And if you want to save the value ofiacross multiple pages or still having the value after pressing F5 or reloading the page, useSession.
â VDWWD
1 hour ago
This is working...Thanks Buddy.
â Jitender
1 hour ago
This is working...Thanks Buddy.
â Jitender
1 hour ago
1
1
You're welcome. And if you want to save the value of
i across multiple pages or still having the value after pressing F5 or reloading the page, use Session.â VDWWD
1 hour ago
You're welcome. And if you want to save the value of
i across multiple pages or still having the value after pressing F5 or reloading the page, use Session.â VDWWD
1 hour ago
add a comment |Â
up vote
1
down vote
Check whether the form is posted back and then assign values. Check IsPostBack
private int i;
protected void Timer1_Tick(object sender, EventArgs e)
if (!IsPostBack)
i = 0;
else
i = Int32.Parse(Label3.Text);
i++;
Label3.Text = i.ToString();
this is not working every time it is showing 1
â Jitender
1 hour ago
check my updated code
â mbharanidharan88
1 hour ago
add a comment |Â
up vote
1
down vote
Check whether the form is posted back and then assign values. Check IsPostBack
private int i;
protected void Timer1_Tick(object sender, EventArgs e)
if (!IsPostBack)
i = 0;
else
i = Int32.Parse(Label3.Text);
i++;
Label3.Text = i.ToString();
this is not working every time it is showing 1
â Jitender
1 hour ago
check my updated code
â mbharanidharan88
1 hour ago
add a comment |Â
up vote
1
down vote
up vote
1
down vote
Check whether the form is posted back and then assign values. Check IsPostBack
private int i;
protected void Timer1_Tick(object sender, EventArgs e)
if (!IsPostBack)
i = 0;
else
i = Int32.Parse(Label3.Text);
i++;
Label3.Text = i.ToString();
Check whether the form is posted back and then assign values. Check IsPostBack
private int i;
protected void Timer1_Tick(object sender, EventArgs e)
if (!IsPostBack)
i = 0;
else
i = Int32.Parse(Label3.Text);
i++;
Label3.Text = i.ToString();
edited 1 hour ago
answered 1 hour ago
mbharanidharan88
3,72332253
3,72332253
this is not working every time it is showing 1
â Jitender
1 hour ago
check my updated code
â mbharanidharan88
1 hour ago
add a comment |Â
this is not working every time it is showing 1
â Jitender
1 hour ago
check my updated code
â mbharanidharan88
1 hour ago
this is not working every time it is showing 1
â Jitender
1 hour ago
this is not working every time it is showing 1
â Jitender
1 hour ago
check my updated code
â mbharanidharan88
1 hour ago
check my updated code
â mbharanidharan88
1 hour ago
add a comment |Â
up vote
1
down vote
Generally speaking it is not a good practice to store values inside of views (such as asp.net page). It could be overwritten each time the request is sent.
You could store your data elsewhere:
public static class StaticDataStorage
public static int Counter = 0;
And use it:
protected void Timer1_Tick(object sender, EventArgs e)
StaticDataStorage.Counter++;
Label3.Text = StaticDataStorage.Counter.ToString();
add a comment |Â
up vote
1
down vote
Generally speaking it is not a good practice to store values inside of views (such as asp.net page). It could be overwritten each time the request is sent.
You could store your data elsewhere:
public static class StaticDataStorage
public static int Counter = 0;
And use it:
protected void Timer1_Tick(object sender, EventArgs e)
StaticDataStorage.Counter++;
Label3.Text = StaticDataStorage.Counter.ToString();
add a comment |Â
up vote
1
down vote
up vote
1
down vote
Generally speaking it is not a good practice to store values inside of views (such as asp.net page). It could be overwritten each time the request is sent.
You could store your data elsewhere:
public static class StaticDataStorage
public static int Counter = 0;
And use it:
protected void Timer1_Tick(object sender, EventArgs e)
StaticDataStorage.Counter++;
Label3.Text = StaticDataStorage.Counter.ToString();
Generally speaking it is not a good practice to store values inside of views (such as asp.net page). It could be overwritten each time the request is sent.
You could store your data elsewhere:
public static class StaticDataStorage
public static int Counter = 0;
And use it:
protected void Timer1_Tick(object sender, EventArgs e)
StaticDataStorage.Counter++;
Label3.Text = StaticDataStorage.Counter.ToString();
edited 1 hour ago
answered 1 hour ago
Fabjan
9,02921438
9,02921438
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53151925%2ftimer-tick-not-increasing-values-on-time-interval%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password

For that use-case, you really need to use JavaScript (I guess your actual code is more complex though and needs something server-side)
â Jonathan Twite
1 hour ago