[英]how to create textbox dynamically after click (button)? asp.net
I tried to figure out how to create a textbox (or other controls) dynamically, after click on button. 单击按钮后,我试图弄清楚如何动态创建文本框(或其他控件)。 I have written a function that does work if I add it to the Page_Load
method, but it does not work as related to a specific button (after click). 如果将其添加到Page_Load
方法中,我已经编写了一个函数,但是该函数不能与特定按钮相关(单击后)。
Also, I tried to understand how to count my clicks. 另外,我试图了解如何计算点击次数。
.aspx
markup: .aspx
标记:
<asp:Button ID="addexProductBTN" runat="server" Text="add" onclick="addexProductBTN_Click"/>
<asp:PlaceHolder ID="DynamicControlsHolder" runat="server"></asp:PlaceHolder>
.aspx.cs
code: .aspx.cs
代码:
int count = 0;
protected void addnewProductBTN_Click(object sender, EventArgs e)
{
count++;
addControlTB();
}
protected void addControlTB()
{
Table tbldynamic = new Table();
TableCell tc = new TableCell();
TableRow tr = new TableRow();
TextBox NewProdTB = new TextBox();
NewProdTB.ID = "DynamicTextBox";
NewProdTB.Text = "";
tc.Controls.Add(NewProdTB);
tr.Cells.Add(tc);
tbldynamic.Rows.Add(tr);
DynamicControlsHolder.Controls.Add(tbldynamic);
}
the first thing has to do with viewstate: 第一件事与viewstate有关:
TextBox NewProdTB = new TextBox();
NewProdTB.EnableViewState = false;
Try to play around how the behaviour changes, when set to false and true; 设置为false和true时,尝试解决行为的变化。
THIS here is an article about the viewstate behaviour. 这是有关视图状态行为的文章。
The "count" part of your question: if you need a global variable that survives reload of the page, you should try to use session object . 问题的“计数”部分:如果您需要一个在重新加载页面后仍然有效的全局变量,则应尝试使用session object 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.