Timer_Tick event increments variable only the first time it happens

Last post 12-05-2007 3:11 PM by gt1329a. 1 replies.

Sort Posts:

  • Timer_Tick event increments variable only the first time it happens

    12-05-2007, 11:05 AM
    • Member
      point Member
    • pzelenovic
    • Member since 12-05-2007, 3:55 PM
    • Posts 1

    Hello everyone,

    I am trying to write a script that will rotate the news in one <div> with the use of ajax timer. I am trying to use a public variable named "num" in order to have different news shown with every tick event. The problem is that when I run the program, the browser shows the first news, then changes to the second one, and then afterwards the variable "num" never increments. I have added a label which receives the current time (Label1.Text = DateTime.Now.ToString();) with every tick and that works properly, but when I did the same with the num variable, I have seen that the number increments to number 2 and then stops incrementing.

    This is the code:

    using System;
    using System.Data;
    using System.Data.SqlClient;
    using System.Configuration;
    using System.Collections;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;
    
    public partial class RotatingNews : System.Web.UI.Page
    {
        public int num = 1;
    
        protected void Page_Load(object sender, EventArgs e)
        {
            SqlConnection cn = new SqlConnection();
            cn.ConnectionString = "Data Source=.;Initial Catalog=Knjige;Integrated Security=True";
    
            SqlCommand cm = new SqlCommand();
            cm.Connection = cn;
            cm.CommandText = "Select Naslov, Tekst from Vesti where id = " + num;
    
            SqlDataReader rdr;
    
            cn.Open();
            rdr = cm.ExecuteReader();
            Repeater1.DataSource = rdr;
            Repeater1.DataBind();
            rdr.Close();
            cn.Close();
        }
        
        protected void Timer1_Tick(object sender, EventArgs e)
        {
           
                broj++;
                SqlConnection cn = new SqlConnection();
                cn.ConnectionString = "Data Source=.;Initial Catalog=Knjige;Integrated Security=True";
    
                SqlCommand cm = new SqlCommand();
                cm.Connection = cn;
                cm.CommandText = "Select Naslov, Tekst from Vesti where id = " + broj;
    
                SqlDataReader rdr;
    
                cn.Open();
                rdr = cm.ExecuteReader();
                Repeater1.DataSource = rdr;
                Repeater1.DataBind();
                rdr.Close();
                cn.Close();
                
                Label1.Text = broj.ToString();
                Label2.Text = DateTime.Now.ToString();
                
        }
        
    }
    
    Does anybody have an idea why this is not working properly?
     
  • Re: Timer_Tick event increments variable only the first time it happens

    12-05-2007, 3:11 PM
    Answer
    • Star
      13,605 point Star
    • gt1329a
    • Member since 06-24-2002, 12:53 AM
    • Atlanta
    • Posts 2,248
    • ASPInsiders
      TrustedFriends-MVPs

    Declaring the variable as public doesn't cause it to persist across postbacks.  Try something like this:

    public int num
    {
    get { if (string.IsNullOrEmpty(ViewState["num"])
    {
    ViewState["num"] = "1";
    return 1;
    }
    else { return Int32.Parse(ViewState["num"]);
    }
    }
    set { ViewState["num"] = value.ToString(); }
    }
    Encosia - ASP.NET, jQuery, AJAX, and more.

    Latest article: Emulate ASP.NET validation groups with jQuery validation
Page 1 of 1 (2 items)
Microsoft Communities