每个按钮单击C.ASP.NET的增量数 [英] Increment number per button click C# ASP.NET

查看:79
本文介绍了每个按钮单击C.ASP.NET的增量数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复: 按钮单击事件无法正常工作

我尝试为默认页面上的每次点击增加一个int值. Int=0.最多只能达到1.我该怎么做才能增加每次点击的次数?

I try to increment an int for each click on a default page. Int=0. It only goes up to 1. What should I do to increment the number for each click?

public partial class _Default : System.Web.UI.Page
{
    private int speed = 0;

    public int Speed
    {
        get { return speed; }  // Getter
        set { speed = value; } // Setter
    }

    public void accelerate()
    {
        //speed++;
        this.Speed = this.Speed + 1;
    }

    public void decelerate()
    {
        // speed--;
        this.Speed = this.Speed - 1;
    }

    public int showspeed()
    {
        return this.Speed;
    }

    //car bmw = new car();
    public void Page_Load(object sender, EventArgs e)
    {
        //datatype objectname = new

       dashboard.Text = Convert.ToString(this.showspeed());
    }

    public void acc_Click(object sender, EventArgs e)
    {
       this.accelerate();
       dashboard.Text = Convert.ToString(this.showspeed());
    }

    public void dec_Click(object sender, EventArgs e)
    {
        this.decelerate();
        this.showspeed();
    }
}

推荐答案

您可以使用

You could use the ViewState to maintain the value across postbacks:

private int Speed
{
   get
   {
       if (ViewState["Speed"] == null)
           ViewState["Speed"] = 0;
       return (int)ViewState["Speed"];
   }
   set { ViewState["Speed"] = value; }
}

这篇关于每个按钮单击C.ASP.NET的增量数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆