I have some problems getting the string from some textbox that I added using a linkbutton. This code can add textboxes by clicking on linkbutton and these textboxes are added on a panel, but I don't know how to get the string that the user writes in these textboxes that were added, and also I can't keep the string that were written in the textbox when I click on the linkbutton to add one more textbox. Can you help me please? Here is my code:
public partial class _Default : Page
{
Label myLabel1;
Label myLabel2;
protected void Page_Load(object sender, EventArgs e)
{
myLabel1 = new Label();
myLabel2 = new Label();
Panel1.Controls.Add(myLabel1);
Panel2.Controls.Add(myLabel2);
if (!Page.IsPostBack)
{
//Remove the session when first time page loads.
Session.Remove("clicks");
Session.Remove("clicks2");
}
}
private void BuildTextBoxes(int rowCount1, int rowCount2)
{
for (int i = 0; i < rowCount1; i++)
{
TextBox TxtBoxU = new TextBox();
TxtBoxU.ID = "TextBoxU" + i.ToString();
//Add the labels and textboxes to the Panel.
Panel1.Controls.Add(TxtBoxU);
}
myLabel1.Text = rowCount1 + "";
for (int i = 0; i < rowCount2; i++)
{
TextBox TxtBoxU = new TextBox();
TxtBoxU.ID = "TextBoxU" + i.ToString();
//Add the labels and textboxes to the Panel.
Panel2.Controls.Add(TxtBoxU);
}
myLabel2.Text = rowCount2 + "";
}
protected void LinkButton1_Click(object sender, EventArgs e)
{
int rowCount1 = 0;
//initialize a session.
rowCount1 = Convert.ToInt32(Session["clicks"]);
rowCount1++;
//In each button clic save the numbers into the session.
Session["clicks"] = rowCount1;
BuildTextBoxes(rowCount1, Convert.ToInt32(Session["clicks2"]));
}
protected void LinkButton2_Click(object sender, EventArgs e)
{
int rowCount2 = 0;
//initialize a session.
rowCount2 = Convert.ToInt32(Session["clicks2"]);
rowCount2++;
//In each button clic save the numbers into the session.
Session["clicks2"] = rowCount2;
BuildTextBoxes(Convert.ToInt32(Session["clicks"]), rowCount2);
}
}
Thank you so much.