简体   繁体   English

单击按钮时的增量计数C#Visual Studio 2013

[英]increment count when button is clicked C# Visual Studio 2013

So, i am trying to increment a count every time i click a button. 因此,每次单击按钮时,我都会尝试增加计数。 And as for the count, i want to save it into an arraylist, after that, i want to retrieve the database by using the last row of arraylist as the ID 至于计数,我想将其保存到arraylist中,之后,我想使用arraylist的最后一行作为ID来检索数据库。

public partial class Queue : System.Web.UI.Page
{
    ArrayList array = new ArrayList();
    int count = 0;

    protected void Page_Load(object sender, EventArgs e)
    {
        LblDate.Text = DateTime.Today.ToString("dd-MM-yyyy");
        array.Add(count);
    }

    protected void Timer1_Tick(object sender, EventArgs e)
    {
        LblTime.Text = DateTime.Now.ToString("HH:mm:ss");
    }

    protected void Button1_Click(object sender, EventArgs e)
    {  
        count++;
        array.Add(count);
        int last = Convert.ToInt32(array[array.Count - 1]);

        queue fmCustomer = new queue();
        fmCustomer = fmCustomer.getQueueNoByID(last.ToString());

        Label1.Text = last.ToString();
    }
}

These are my codes, the problem i am facing now is i can only increment once for the count. 这些是我的代码,我现在面临的问题是我只能递增一次。

When i click the button, it display 1, but after that it wont increase again 当我单击按钮时,它显示1,但此后它将不再增加

So i would need some help with that. 所以我需要一些帮助。

I see a few issues with the code.. 我发现代码有一些问题。

  1. Why is count set to 1 and not 0? 为什么将计数设置为1而不是0?
  2. What is the purpose of button1WasClicked button1WasClicked的用途是什么
  3. What do you plan to do with this array list - even if your code was working it would just be a list of 0,1,2 ... N where N = number of clicks 您打算如何处理此数组列表-即使您的代码在工作,它也只会是0,1,2 ... N的列表,其中N =点击次数

Button1_Click event will be fired when you click the button. 当您单击按钮时,将触发Button1_Click事件。 You don't need to check button1WasClicked == true 您不需要检查button1WasClicked == true

You are using a Page class. 您正在使用Page类。 This class will be destroyed after your request response. 您的请求响应后,该类将被销毁。 If will be recreated for the next request. 将为下一个请求重新创建。

Therefore button1WasClicked will always be false, and array will always be initialized (and therefore empty). 因此,button1WasClicked将始终为false,并且数组将始终被初始化(因此为空)。

The Page_Load event will always fire on every request. Page_Load事件将始终在每个请求上触发。 This will cause your array to always get the value 1. 这将使您的数组始终获得值1。

You need to step back and take some time to understand the page lifecycle for aspx. 您需要退后一步,花一些时间来了解aspx的页面生命周期。 Also you can set brakepoints and inspect what your code is doing. 另外,您可以设置刹车点并检查代码在做什么。

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

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