asp.netwebformscounterpage-refreshbuttonclick

incrementing a counter by asp.net button click


Is there a way to press an ASP.NET button on a web form and incrementing a counter on each button click? (displaying it on a label for example)

so when trying:

namespace TestButtonClick
{
    public partial class WebForm1 : System.Web.UI.Page
    {
          int counter = 1;
          protected void Page_Load(object sender, EventArgs e)
          {
            
          }
          protected void Button1_Click(object sender, EventArgs e)
          {
              Label1.Text = counter.ToString();
              counter++;
          }
    }
}

The Problem is that the page is refreshing after the button click and the counter is not incrementing.


Solution

  • Remember, AFTER the page is dished out to the browser( the client ), the server side class, code, memory and variables are tossed out - DO NOT exist anymore. The web server is now waiting for you to post back, and maybe 20 other users of the web site. When THEY click some button, their web page is send to the ONE web server, the page class is RE-CREATED, all code/class variables are re-initialized to their scratch default values, THEN the code runs, page is modified, send back to client side, and the EIXSTING server side page, code, memory and variables NOW go out of scope (very much like calling a sub routine).

    The web server does NOT keep a copy of that page - it just sitting there waiting for ANY user and ANY post-back from ANY of the we users on that site. There is NO state, no persisting of code values, and each button click or post back means the WHOLE page class is re-initialized and started as brand new EACH time!!! Best to think of a button page post-back like calling some sub routine each time - it starts out from scratch each time.

    As suggested here, you need then to persist the value.

    Many ways to do this:

    Use a text box, or label for this - they have automatic persisting of values

    Use (drop) into the page a hidden field - they persist.

    Shove (save) the value of the variable into session(). This is server side memory and persisting.

    Shove (save) the value of the variable into viewstate. View state is browser (client side) based.

    So, you could do this:

    So, most simple, is to use the value of the label for this.

    So, we have this:

            <asp:Label ID="Label1" runat="server" Text="0"></asp:Label>
            <br />
    
            <asp:Button ID="Button1" runat="server" Text="add 1" OnClick="Button1_Click" />
    

    And code behind would be this to +1 the label on each button click:

        protected void Button1_Click(object sender, EventArgs e)
        {
            int MyCount = Convert.ToInt32(Label1.Text);
    
            MyCount += 1;
    
            Label1.Text = MyCount.ToString();
        }
    

    so above can work.

    Or:

    Using a variable - but saving in view state.

        int MyCount = 0;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                ViewState["MyCount"] = MyCount;
                Label1.Text = MyCount.ToString();
            }
            else
            { 
                MyCount = (int)ViewState["MyCount"];        
            }
        }
        protected void Button1_Click(object sender, EventArgs e)
        {
            MyCount += 1;
            Label1.Text = MyCount.ToString();
            ViewState["MyCount"] = MyCount;
        }