简体   繁体   English

每次单击按钮时递增数字

[英]Increment number every time button is click

I want to increase an int variable i whenever I click on a button.每当我单击按钮时,我都想增加一个int变量i But what I get is only int value of 1 and it doesn't increase anymore.但我得到的只是int1并且不再增加。

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 .所以你的非静态变量将被重置为0

You can define i as static:您可以将i定义为静态:

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.但请注意i变量在所有用户之间共享。 To improve this issue, you can use Session .要改善此问题,您可以使用Session

Session is an ability to store data of each user in a session. Session是在会话中存储每个用户的数据的能力。

So you can use following property to change the i variable in each session:因此,您可以使用以下属性来更改每个会话中的i变量:

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您必须在webform中将变量保存在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.每次将 lblStart.Text 值转换为 int 并将其分配给 i。 Then increase i.然后增加 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.您可以使用隐藏字段,将它们初始化为 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 !它对我有用,但在 page_load() 上它再次从 1 开始值!

this is actually my first time doing this这实际上是我第一次这样做

        int i = 0;

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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