C# 动态创建标签

C# 动态创建标签,c#,asp.net,C#,Asp.net,我想在每次单击按钮时动态生成一个带有数字的标签。 我希望每当单击按钮时,标签都会生成连续的数字。(一次一个标签) 我在发布我在下面写的代码,但是,在执行这段代码时,只生成了一个标签。onclick事件似乎并没有触发那个事件。 请帮忙 using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebCo

我想在每次单击按钮时动态生成一个带有数字的标签。 我希望每当单击按钮时,标签都会生成连续的数字。(一次一个标签) 我在发布我在下面写的代码,但是,在执行这段代码时,只生成了一个标签。onclick事件似乎并没有触发那个事件。 请帮忙



using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;


namespace WebApplication3
{
    public partial class WebForm17 : System.Web.UI.Page
    {

            int x = 400;
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        //user defined function 
        void GenerateLabel ()
        {
            Label l = new Label();
            Panel1.Controls.Add(l);
            l.Style["top"] = "25px";
            l.Style["Left"] = "60px";
            l.Text = this.x.ToString();
            x = x + 1;

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            GenerateLabel();
        }
    }
}
了解。若变量应在对象的所有实例中保持其值,则将其声明为静态变量。这将在每次单击时增加x

static int x = 400;