title: How to make timer event start on button click in aspx website
body: <p>i am working on an aspx website in which i am using a timer event.
  i just want that timer should run when i click a specific button. now timer is running on page load. in Winforms we use timer.Tick() event to do it. but it is not supporting in website.
  Can Anyone help me to sort this Out.
  Thanks in Advance....</p>
  
  <p>My Code is Here....</p>
  
  <pre><code>    using System;
      using System.Collections.Generic;
      using System.Linq;
      using System.Web;
      using System.Web.UI;
      using System.Web.UI.WebControls;
      using System.Data.SqlClient;
      using System.Configuration;
      using System.ComponentModel;
  
  
      public partial class Expert : System.Web.UI.Page
      {
       public static BackgroundWorker worker = new BackgroundWorker();
  
        protected void Page_Load(object sender, EventArgs e)
            {
             int id;
             id = Int32.Parse(Request.QueryString["id"]);
             SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["LPSConnection"].ConnectionString.ToString());
          con.Open();
          SqlDataReader rd;
          rd = new SqlCommand("Select * from ExpertChat where id=" + id, con).ExecuteReader();
          rd.Read();
          Image1.ImageUrl = rd["image1"].ToString();
  
      Label8.Text = rd["image1"].ToString();
      //Image3.ImageUrl = rd["image1"].ToString();
          Label1.Text = rd["SName"].ToString();
          Label2.Text = rd["Skills"].ToString();
          Label5.Text = rd["rate"].ToString();
          Label3.Text = rd["ReviewCount"].ToString();
          Label4.Text = rd["Title"].ToString();
          Label6.Text = rd["ReviewCount"].ToString();
          Label7.Text = rd["Title"].ToString();
          Label9.Text = rd["Qualification"].ToString();
          Label10.Text = rd["MyServices"].ToString();
          Label11.Text = rd["other"].ToString();
          Label14.Text = rd["IsLoggedIn"].ToString();
  
      int x = Int32.Parse(rd["IsLoggedIn"].ToString());
          if (x == 1)
          {
              Image2.ImageUrl = "~/online.png";
          }
  
          else
          {
              Image2.ImageUrl = "~/offline.png";
          }
          rd.Close();
          if (Session["User"] == "User")
          {
              SqlDataReader rd1 = new SqlCommand("Select funds from signup where email='" + Session["email"].ToString() + "'", con).ExecuteReader();
              rd1.Read();
              Label13.Text = rd1["funds"].ToString();
          }
          worker.DoWork += new DoWorkEventHandler(DoWork);
          worker.WorkerReportsProgress = true;
          worker.WorkerSupportsCancellation = true;
  
          con.Close();
  
      }
  
  protected void Button2_Click(object sender, EventArgs e)
  {
  
  }
  
  protected void SendButton_Click(object sender, EventArgs e)
  {
      string messageMask = "{0} @ {1} : {2}";
      string message = string.Format(messageMask, "yash", DateTime.Now.ToShortTimeString(), NewMessageTextBox.Text);
      TextBox1.Text = TextBox1.Text + Environment.NewLine + message;
      // Calling the DoWork Method Asynchronously
      ScriptManager.RegisterStartupScript(this.Page, this.Page.GetType(), "myScript", "document.getElementById('" + NewMessageTextBox.ClientID + "').value = '';", true);
      ScriptManager.RegisterStartupScript(this, this.GetType(), "TextBox1slide", "buttonClicked();", true);
  
      if (worker.IsBusy != true)
      {
  
          worker.RunWorkerAsync(new string[] { message, Label3.Text });
  
  
      }
  
  
  }
  
  
  
  private static void DoWork(object sender, DoWorkEventArgs e)
  {
      string[] args = e.Argument as string[];
      string value = args[0];
      string id = args[1];
      SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["LPSConnection"].ConnectionString.ToString());
      con.Open();
      SqlCommand cmd;
      cmd = new SqlCommand("Update Chat set Data=@message,Updated1=1 where id=" + Int32.Parse(id), con);
      cmd.Parameters.AddWithValue("@message", value);
  
      cmd.ExecuteNonQuery();
      con.Close();
  }
  
  protected void ChatTextTimer_Tick(object sender, EventArgs e)
  {
      SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["LPSConnection"].ConnectionString.ToString());
      con.Open();
  
      //if (second1.Visible)
      //{
      //    SqlDataReader rd7 = new SqlCommand("Select IsApproved from Chat where id=" + Int32.Parse(Label2.Text) + "'", con).ExecuteReader();
      //    rd7.Read();
      //    string str3 = rd7["IsApproved"].ToString();
      //    rd7.Close();
      //    if (str3 == "Approved")
      //    {
      //        second1.Visible = false;
      //    }
      //    else if (str3 == "Canceled")
      //    {
      //        second1.Visible = false;
      //        second3.Visible = true;
  
  
      //    }
  
      //}
      //else
      //{
      int x1 = 0;
      SqlDataReader rd;
      rd = new SqlCommand("Select UserInitial,Updated from Chat where id =" + Int32.Parse(Label3.Text), con).ExecuteReader();
      rd.Read();
      string str;
      int i;
      i = Int32.Parse(rd["Updated"].ToString());
      if (i == 1)
      {
          str = rd["UserInitial"].ToString();
          rd.Close();
  
          x1 = x1 + 1;
          TextBox1.Text = TextBox1.Text + Environment.NewLine + str;
          SqlCommand
          cmd = new SqlCommand("Update Chat set Updated=0 where id=" + Int32.Parse(Label3.Text), con);
          cmd.ExecuteNonQuery();
  
      }
      else
      {
          rd.Close();
      }
      con.Close();
      ScriptManager.RegisterStartupScript(this, this.GetType(), "TextBox1slide", "buttonClicked();", true);
  
  }
  
  protected void TextBox1_TextChanged(object sender, EventArgs e)
  {
  
  }
  
  
  
  protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
  {
  
      if (Session["User"] != "User")
      {
          Response.Redirect("signin.aspx");
      }
      else
      {
          if (Int32.Parse(Label13.Text) &lt; 20)
          {
              Response.Redirect("Payment.aspx");
          }
          else
          {
              SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["LPSConnection"].ConnectionString.ToString());
              con.Open();
              SqlDataReader rd = new SqlCommand("Select Booked,email from ExpertChat where id=" + Request.QueryString["id"].ToString(), con).ExecuteReader();
              rd.Read();
              int x = Int32.Parse(rd["Booked"].ToString());
              string str = rd["email"].ToString();
              rd.Close();
              if (x == 0)
              {
                  //second1.Visible = true;
  
                  SqlDataReader mRead1, mRead3, mRead4;
                  mRead1 = new SqlCommand("insert into chat (ExpertName,UserName,rate,Data,Date,UserInitial) Values ('" + str + "','" + Session["SName"] + "','" + Label5.Text + "','Hello','" + DateTime.Now.ToShortDateString() + "','yash')", con).ExecuteReader();
                  mRead1.Close();
                  mRead3 = new SqlCommand("Update ExpertChat Set Booked=1 where SName='" + Label1.Text + "'", con).ExecuteReader();
                  mRead3.Close();
                  mRead4 = new SqlCommand("Select top 1 id from Chat  where ExpertName='" + str + "' Order by id desc", con).ExecuteReader();
                  mRead4.Read();
                  int y = Int32.Parse(mRead4["id"].ToString());
                  mRead4.Close();
                  ScriptManager.RegisterStartupScript(this, this.GetType(), "TextBox1slide", "button1Clicked();", true);
                  ChatTextTimer.Enabled = true;
  
                  second3.Visible = false;
                      second1.Visible = false;
                      second2.Visible = false;
                      int id;
                      id = y;
                      Label3.Text = id.ToString();
                      SqlDataReader rd1 = new SqlCommand("Select ExpertName from Chat where id=" + y, con).ExecuteReader();
                      rd1.Read();
                      string str23 = rd1["ExpertName"].ToString();
                      rd1.Close();
                      SqlDataReader rd3;
                      rd3 = new SqlCommand("Select * from ExpertChat where email='" + str23 + "'", con).ExecuteReader();
                      rd3.Read();
                      string str2;
                      str2 = rd3["email"].ToString();
                      Label1.Text = rd3["rate"].ToString();
                      Image3.ImageUrl = rd3["image1"].ToString();
                      Label12.Text = rd3["rate"].ToString();
                      rd3.Close();
                      con.Close();
  
  
  
              }
              else
              {
                  ScriptManager.RegisterStartupScript(this, this.GetType(), "TextBox1slide", "buttonClicked();", true);
                  //second2.Visible = true;
              }
  
  
          }
      }
  
  
  
  
  }
  
        }
  </code></pre>
  
  <p>i want to run it when ImageButton1_Click event Fired....</p>