【问题标题】:Button (clicked) work one time按钮(单击)工作一次
【发布时间】:2018-02-13 13:54:38
【问题描述】:

WebForm1.aspx.cs

   namespace WebApplication1
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        private int n = 0;
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            ++n;
            Button1.Text = n.ToString();
        }
    }
}

WebForm1.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication1.WebForm1" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>

        <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" />

    </div>
    </form>
</body>
</html>

'n'改变一次

谢谢 ///////////////////////////////////////// ///////////////////////////////////////// ///////////////////////////////////////// ////

【问题讨论】:

  • 做一些关于 ASP.NET 的基本阅读

标签: c# asp.net webforms


【解决方案1】:

你需要把它变成静态的

 namespace WebApplication1
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        private static int n = 0;
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            ++n;
            Button1.Text = n.ToString();
        }
    }
}

【讨论】:

    【解决方案2】:

    n 声明为静态变量并检查。它会工作

    private static int n = 0;
    

    【讨论】:

      猜你喜欢
      • 2012-12-11
      • 2018-11-22
      • 1970-01-01
      • 2016-10-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多