how to count the number of button clicks?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • raghurocks
    New Member
    • May 2012
    • 46

    how to count the number of button clicks?

    How do you count the number of button clicks?
    Code:
    int count;
    if (Label1.Text == "")
    {
      Label1.Text = (count + 1).ToString();
      count++;
    
    }
    else
    {
      count = Convert.ToInt32(Label1.Text);
      Label1.Text = (count + 1).ToString();
    }
    Last edited by Frinavale; May 29 '12, 01:53 PM. Reason: Added code tags to the posted code and added the question into the body of the thread.
  • raghurocks
    New Member
    • May 2012
    • 46

    #2
    the count should be declared globally....... .....

    Comment

    • Frinavale
      Recognized Expert Expert
      • Oct 2006
      • 9749

      #3
      You could retrieve the count form the label using the Int32.TryParse method and go from there:
      Code:
      int count;
      Int32.TryParse(Label1.Text, out count);
      count++;
      Label1.Text = count.ToString();

      Comment

      • raghurocks
        New Member
        • May 2012
        • 46

        #4
        ya...thank u,i will try finvarle

        Comment

        • sialon
          New Member
          • Jun 2012
          • 3

          #5
          You can create a table in a database id and count
          for button
          Code:
          protected void Button1_Click(object sender, EventArgs e)
              {
                  
                  try
                  {
                      if (Session["mode"] == "true")
                      {
                         
                          SqlConnection con = new SqlConnection("");
                          SqlCommand cmd = new SqlCommand("select * from TB_Count", con);
                          con.Open();
                          SqlDataReader sdr = cmd.ExecuteReader();
                          sdr.Read();
                          int super = int.Parse(sdr["count"].ToString());
                          super++;
                          string str = "update TB_Count set Count='" + super + "'";
                          con.Close();
                          SqlConnection con1 = new SqlConnection(conn);
                          SqlCommand cmd1 = new SqlCommand(str, con1);
                          con1.Open();
                          cmd1.ExecuteNonQuery();
                          con1.Close();
                          Label2.Text = " NumberDownloads:" + super;
                          Response.Redirect(" address file");
                      }
                      else
                      {
                          Button1.Visible = false;
                          Button2.Visible = true;
                          Label4.Visible = true;
                      }
          for pageload
          Code:
          SqlConnection con = new SqlConnection(conn);
                          SqlCommand cmd = new SqlCommand("select * from TB_Count", con);
                          con.Open();
                         SqlDataReader sdr = cmd.ExecuteReader();
                          sdr.Read();
                         int super = int.Parse(sdr["Count"].ToString());
                          con.Close();
                         Label2.Text = "NumberDownloads:" + super;

          Comment

          Working...