C# asp.net-如何在单击按钮时动态添加控件?
我试图添加控件,但它正在覆盖 在下图中,如果单击添加更多Visa按钮,我希望显示所有这些字段 代码:C# asp.net-如何在单击按钮时动态添加控件?,c#,asp.net,webforms,C#,Asp.net,Webforms,我试图添加控件,但它正在覆盖 在下图中,如果单击添加更多Visa按钮,我希望显示所有这些字段 代码: 签证号码: 国家名称: 签证类型: 条目类型: 到期日 代码.cs: static int i = 0; protected void btnAddVisa_Click(object sender, EventArgs e) { i++; for (int j = 0; j <= i; j++) {
签证号码:
国家名称:
签证类型:
条目类型:
到期日
代码.cs:
static int i = 0;
protected void btnAddVisa_Click(object sender, EventArgs e)
{
i++;
for (int j = 0; j <= i; j++)
{
PlaceHolder1.Controls.Add(divReg);
PlaceHolder1.Controls.Add(new LiteralControl("<BR>"));
}
}
static int i=0;
受保护的无效btnAddVisa_单击(对象发送方,事件参数e)
{
i++;
对于(int j=0;j创建一个用户控件来表示所有添加的控件,然后将控件作为一个整体添加。确保使用LoadControl方法,而不是控件的构造函数。您还需要在ViewState中保留足够的信息,以便在加载页面上重新创建控件。在您的情况下,仅控件的数量就足够了
static int i = 0;
protected void btnAddVisa_Click(object sender, EventArgs e)
{
i++;
for (int j = 0; j <= i; j++)
{
PlaceHolder1.Controls.Add(divReg);
PlaceHolder1.Controls.Add(new LiteralControl("<BR>"));
}
}