首页 > 解决方案 > 动态创建标签

问题描述

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

标签: c#asp.net

解决方案


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

static int x = 400;

推荐阅读