如何在回发中保持运行总计? [英] How to keep a running total on postback?
本文介绍了如何在回发中保持运行总计?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个文本框,用户可以在其中输入分数。单击按钮时,将在列表框中输入该分数,并清除文本框。
问题是我想保持一个总计。例如,如果输入3,则总数应为3,接下来输入7时,总数应为10.
我尝试过:
I have a a textbox where the user would enter a score. When a button is click that score would be entered in a listbox and the textbox would be cleared.
The problem is that I want to keep a running total. For example if 3 is entered then total should be 3, next when 7 is entered total should be 10.
What I have tried:
int total;
ViewState["total"] = 0;
int num = Convert.ToInt32(txtScore.Text);
total = total + num;
Label1.Text = total.ToString();
推荐答案
你可以通过一系列不同的方式来做,比如上面的隐藏字段。要使用ViewState执行此操作,我会执行以下操作。
You can do it a bunch of different ways, such as hidden fields above. To do it using ViewState I would do the following.
int total;
int num = Convert.ToInt32(txtScore.Text);
if (ViewState["total"] == null)
{
total = 0;
}
else
{
total = (int)ViewState["total"] + num;
}
ViewState["total"] = total;
Label1.Text = total.ToString();
使用隐藏字段 [ ^ ]
这篇关于如何在回发中保持运行总计?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文