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).
Also, I tried to understand how to count my clicks.
.aspx
markup:
<asp:Button ID="addexProductBTN" runat="server" Text="add" onclick="addexProductBTN_Click"/>
<asp:PlaceHolder ID="DynamicControlsHolder" runat="server"></asp:PlaceHolder>
.aspx.cs
code:
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:
TextBox NewProdTB = new TextBox();
NewProdTB.EnableViewState = false;
Try to play around how the behaviour changes, when set to false and 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 .
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.