c# - 动态创建标签
问题描述
每当单击按钮时,我想动态生成带有数字的标签。我希望在单击按钮时生成带有连续数字的标签。(一次一个标签)我发布了我在下面编写的代码,但是,在执行此代码时,只会生成一个标签。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;
推荐阅读
- workflow - 使用 activiti 将工作流分配给 Alfresco 中的组
- python-3.x - Groupby 扩展计数 - 不同时间戳组的元素变化
- python - 如何使用自定义时区在 django 中根据时间过滤查询集?
- python - 如何生成一个随机数,将它们放入字典中,然后返回重复次数
- go - 在 helm 模板中将千兆字节转换为字节
- ios - 如何存储和访问 NSAttributedString.Key.foregroundColor 值的“数组”
- c - 为什么这个循环永远存在?
- java - 只需要更新java中mongo数组中的特定元素
- xml - 错误:文档中没有引用语法约束(DTD 或 XML 模式)
- pytorch - 将整数转换为二进制位的 pytorch 张量