2

I've done a bit of C++ programming but I'm new to c#.

I wrote a simple app to count how many times a user clicked a button and then display that count on the button.

On the first click it works, but after that it stays at 1. Any suggestions? Thanks in advance

public partial class Default : System.Web.UI.Page
{
    private int clickcount = 0;
    public virtual void button1Clicked (object sender, EventArgs args)
    {
        clickcount++;
        button1.Text = "You clicked me "+clickcount.ToString()+" time";
    }
    public virtual void GreetButtonClicked (object sender, EventArgs args)
    {
        GreetButton.Text = "Hello "+TextInput.Text;
    }
     }      
4

6 回答 6

4

You are setting the count to zero every time the page is created. This:

private int clickcount = 0;

Is almost the same as this:

private int clickcount;

public Default()
{
    clickcount = 0;
}

Since the constructor is called every time a page is requested, you'll always reset the value back to zero.

How you solve this depends on what you are trying to count. For example, if you want to count the number of clicks in the current session then you should use a session variable to store the count.

I suggest you read the following webpage to get a good overview of where state can be stored and which type of storage is most suitable for different purposes:

于 2012-06-10T18:51:44.610 回答
2

网页不会自动持久化数据,因此 的值clickcount仅保存在内存中,直到服务器完成页面呈现。下次单击该按钮时,您的计数器将重置为 0。如果您想在回发(页面加载)之间保留数据,则必须将数据存储在某处,例如SessionViewState或数据库中。

这是一个示例,从您的代码修改:

public partial class Default : System.Web.UI.Page 
{ 
    private int clickcount = 0; 
    public virtual void button1Clicked (object sender, EventArgs args) 
    { 
        if (Session["clickcount"] != null)
            clickcount = (int)Session["clickcount"];

        clickcount++; 
        Session["clickcount"] = clickcount;

        button1.Text = "You clicked me "+clickcount.ToString()+" time"; 
    } 
    public virtual void GreetButtonClicked (object sender, EventArgs args) 
    { 
        GreetButton.Text = "Hello "+TextInput.Text; 
    } 
}
于 2012-06-10T18:54:29.280 回答
1

clickcount is going to be set to 0 every time you click the button, because it is initialized on every postback to the value you set (in this case --> 0). If you would make it a private static int, you will see that the values will actually increment.

    int counter = 0; <-- gets initialized to 0 on each click
    static int counter1 = 0; <--- gets incremented on each click, so 1,2,3 ...

As it has been pointed out in the comments of this answer: If you plan on actually using the counter, it would be better to use a Session variable to store the amount of times the button has been clicked.

于 2012-06-10T18:51:44.547 回答
1

如果您不关心会话和其他类型的持久性,则可以使用 VIEWSTATE 将此值保存在属性中。

像这样使用您的代码,这将起作用:

public partial class Default : System.Web.UI.Page
{
    public int clickcount 
  {
   get
   {
     if(Viewstate["clickcount"] != null)
     {
      return (int)Viewstate["clickcount"]; //Need to cast the viewstate object in int.
     }
     else
     {
        return 0;
      }
  }
   set
   {
     Viewstate.add("clickcount",value);
   }
  }
    public virtual void button1Clicked (object sender, EventArgs args)
    {
        clickcount++;
        button1.Text = "You clicked me "+clickcount.ToString()+" time";
    }
    public virtual void GreetButtonClicked (object sender, EventArgs args)
    {
        GreetButton.Text = "Hello "+TextInput.Text;
    }
     }
于 2012-06-10T18:57:44.550 回答
0

You should declare clickcount as a static variable.

于 2012-06-10T18:51:33.217 回答
0

我认为有一个简单的方法可以解决这个问题

您可以添加一个新标签并使可见 = false

那么代码会像

counter = int.parse(label.text);
counter++;
label.text = counter.ToString();
button.text = "you clicked me "+counter.ToString();
于 2012-06-10T19:59:35.803 回答