如何在一个按钮内执行On =“ 1”和Off =“ 2”?

时间:2019-08-05 03:42:47

标签: c# asp.net

我在使用两个按钮来表示“打开”和“关闭”。当我按下“开”按钮时,它将传递字符串值“ 1”,而按下“关”按钮时将传递字符串值“ 2”。

如何在一个按钮内完成此操作?默认情况下,按钮应该处于关闭状态,当我按下它时应该处于打开状态,而当我再次按下时它应该处于关闭状态。

public partial class _Default : System.Web.UI.Page
{
    SerialPort ardo;

    protected void Page_Load(object sender, EventArgs e)
    {
        ardo = new SerialPort();
        ardo.PortName = "COM5";
        ardo.BaudRate = 9600;
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        string stop = "1";
        ardo.Open();
        ardo.Write(stop);
        ardo.Close();
    }

    protected void Button2_Click(object sender, EventArgs e)
    {
        string On = "2";
        ardo.Open();
        ardo.Write(On);
        ardo.Close();
    }
}

3 个答案:

答案 0 :(得分:2)

声明一个字符串变量

private string isOn = "2";

然后在按钮单击事件上可以像处理

protected void Button1_Click(object sender, EventArgs e)
{
    if(isOn.Equals("2"))
        isOn = "1";
    else
        isOn = "2";

}

答案 1 :(得分:0)

设置为隐藏文本框。

然后您可以检查是否已按下该语句

V <- c(.25, .5, .75) 
m <- length(V)

r <- matrix(c(1, .3, .3, .3, 1, .3, .3,.3, 1), 3, 3)

sumV <- 0 

for (i in 1:nrow(r)) {
  for (j in 1:nrow(r)) {
    sumV <- sumV + r[i,j] * sqrt(V[[i]]*V[[j]])
     }
   } 

(1/m)^2 * sumV # Final answer

答案 2 :(得分:0)

protected void Button1_Click(object sender, EventArgs e)
    {
       //consider initially its stopped
       if(Session[“currentState”] == null)
           Session[“currentState”] = “1”;

       if(Session[“currentState”].ToString() == “1”)
         {

            Session[“currentState”]= “2”;
         }
         else
         {

            Session[“currentState”]= “1”; 
         }
        ardo.Open();
        ardo.Write(Session[“currentState”].ToString());
        ardo.Close();
    }