动态创建标签

时间:2020-04-09 21:12:28

标签: c# asp.net

我想在每次单击按钮时动态生成带有数字的标签。 我希望每当单击按钮时都使用连续的数字生成标签。 (一次一个标签) 我将发布下面编写的代码,但是,在执行此代码时,仅生成一个标签。 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();
        }
    }
}

1 个答案:

答案 0 :(得分:0)

阅读有关static variables的信息。如果变量应在对象的所有实例中保持其值,则将其声明为静态。每次点击都会增加 x

static int x = 400;