按钮(单击)工作一次

时间:2018-02-13 13:54:39

标签: c# asp.net webforms

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'改变一次

Thank`s ////////////////////////////////////////////////// ////////////////////////////////////////////////// ////////////////////////////////////////////////// ////

2 个答案:

答案 0 :(得分: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();
        }
    }
}

答案 1 :(得分:0)

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

private static int n = 0;