做一个简单的加减1操作
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Web; 5 using System.Web.UI; 6 using System.Web.UI.WebControls; 7 8 public partial class UserControls : System.Web.UI.Page 9 {10 protected void Page_Load(object sender, EventArgs e)11 {12 if (!this.IsPostBack) {13 this.txtCount.Text = "1";14 }15 }16 protected void btnAdd_Click(object sender, EventArgs e)17 {18 this.txtCount.Text = (Convert.ToInt32(txtCount.Text) + 1).ToString();19 }20 protected void btnMinus_Click(object sender, EventArgs e)21 {22 this.txtCount.Text = (Convert.ToInt32(txtCount.Text) - 1).ToString();23 }24 }
*..*Convert.ToInt32:强制类型转换。
解释一下,因为每次用户输入的的数据都是string型的,所以每次以后遇见用户输入结果处理的时候,不要忘了类型装换,(易出错)
重中之重的是IsPostBack
解释如图: