简体   繁体   中英

Increment number every time button is click

I want to increase an int variable i whenever I click on a button. But what I get is only int value of 1 and it doesn't increase anymore.

Here is my code:

private int i;

protected void btnStart_Click(object sender, EventArgs e)
{
    i++;

    lblStart.Text = i.ToString();
}

By each request (Clicking on the button), a new instance will be created. So your non-static variable will be reset to 0 .

You can define i as static:

private static int i;
protected void btnStart_Click(object sender, EventArgs e)
{
    i++;

    lblStart.Text = i.ToString();
}

But please note that the i variable is shared between all the users. To improve this issue, you can use Session .

Session is an ability to store data of each user in a session.

So you can use following property to change the i variable in each session:

private int i
{
    get
    {
        if (Session["i"] == null)
            return 0;

        return (int)Session["i"];

        // Instead of 3 lines in the above, you can use this one too as a short form.
        // return (int?) Session["i"] ?? 0;
    }
    set
    {
        Session["i"] = value;
    }
}

protected void btnStart_Click(object sender, EventArgs e)
{
    i++;

    lblStart.Text = i.ToString();
}

As you know other answer is correct i want add another answer

You must in webform save your variables in ViewState

Just define your variables like this

public int i 
{
    get { Convert.ToInt32( ViewState["i"] ); }
    set { ViewState["i"] = value ; }
}

Convert lblStart.Text value to int every time and assign it to i. Then increase i.

private int i;
protected void btnStart_Click(object sender, EventArgs e)
 {
    i =   Int32.Parse(lblStart.Text);
    i++; 
    lblStart.Text = i.ToString();
 }

I have similar questions as yours and I believe the issue is because the event click did not store the value that has been increased before, therefore it could not be incremented the next time you clicked, so here's my code:

          protected void btn_add_Click(object sender, EventArgs e)
          {
            string initial;
            int increment;
            int quantity;

            initial = TextBoxQty.Text; 
            increment = Convert.ToInt16(initial);
            increment++;
            TextBoxQty.Text = increment.ToString();
            quantity = increment;
            }

You can use a hidden field, initialize them to 0.

private int i;
protected void btnStart_Click(object sender, EventArgs e)
 {
    i =   int.Parse(myHiddenField.Value);
    i++; 
    myHiddenField.Value = i;
    lblStart.Text = i.ToString();
 }
protected static int a = 0;
    
protected void btnStart_Click(object sender, EventArgs e)
{
    a = a+1;
    lblStart.Text = i.ToString();
}

It Works for me but on page_load() it initiates the value from 1 again !

this is actually my first time doing this

        int i = 0;

        while (i>=0)
        {
            Console.WriteLine(i);
            Console.ReadLine();
            i++;
        }
    

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM