Timer Tick not increasing values on time interval

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
6
down vote

favorite
1













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();










share|improve this question























  • 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














up vote
6
down vote

favorite
1













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();










share|improve this question























  • 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












up vote
6
down vote

favorite
1









up vote
6
down vote

favorite
1






1






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();










share|improve this question
















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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
















  • 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












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;






share|improve this answer




















  • This is working...Thanks Buddy.
    – Jitender
    1 hour ago






  • 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

















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();






share|improve this answer






















  • this is not working every time it is showing 1
    – Jitender
    1 hour ago










  • check my updated code
    – mbharanidharan88
    1 hour ago

















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();






share|improve this answer






















    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













     

    draft saved


    draft discarded


















    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






























    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;






    share|improve this answer




















    • This is working...Thanks Buddy.
      – Jitender
      1 hour ago






    • 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














    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;






    share|improve this answer




















    • This is working...Thanks Buddy.
      – Jitender
      1 hour ago






    • 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












    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;






    share|improve this answer












    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;







    share|improve this answer












    share|improve this answer



    share|improve this answer










    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 of i across multiple pages or still having the value after pressing F5 or reloading the page, use Session.
      – VDWWD
      1 hour ago
















    • This is working...Thanks Buddy.
      – Jitender
      1 hour ago






    • 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















    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












    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();






    share|improve this answer






















    • this is not working every time it is showing 1
      – Jitender
      1 hour ago










    • check my updated code
      – mbharanidharan88
      1 hour ago














    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();






    share|improve this answer






















    • this is not working every time it is showing 1
      – Jitender
      1 hour ago










    • check my updated code
      – mbharanidharan88
      1 hour ago












    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();






    share|improve this answer














    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();







    share|improve this answer














    share|improve this answer



    share|improve this answer








    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
















    • 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










    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();






    share|improve this answer


























      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();






      share|improve this answer
























        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();






        share|improve this answer














        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();







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited 1 hour ago

























        answered 1 hour ago









        Fabjan

        9,02921438




        9,02921438



























             

            draft saved


            draft discarded















































             


            draft saved


            draft discarded














            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













































































            Comments

            Popular posts from this blog

            White Anglo-Saxon Protestant

            Is the Concept of Multiple Fantasy Races Scientifically Flawed? [closed]

            One-line joke