Need to add one more textbox beside to the 1st textbox, initially it appears then later it gives error as "Multiple controls with the same ID 'txtDynamic1' were found. FindControl requires that controls have unique IDs."
protected void AddTextBox(object sender, EventArgs e)
{
int index = pnlTextBoxes.Controls.OfType<TextBox>().ToList().Count + 1;
this.CreateTextBox("txtDynamic" + index);
}
private void CreateTextBox(string id)
{
Literal ltitem = new Literal();
ltitem.Text = "ITEM : ";
pnlTextBoxes.Controls.Add(ltitem);
TextBox txtitem = new TextBox();
txtitem.ID = id;
pnlTextBoxes.Controls.Add(txtitem);
Literal ltuom = new Literal();
ltuom.Text = "UOM : ";
pnlTextBoxes.Controls.Add(ltuom);
TextBox txtuom = new TextBox();
txtuom.ID = id;
pnlTextBoxes.Controls.Add(txtuom);
Literal lt = new Literal();
lt.Text = "<br />";
pnlTextBoxes.Controls.Add(lt);
}
protected void Page_PreInit(object sender, EventArgs e)
{
List<string> keys = Request.Form.AllKeys.Where(key => key.Contains("txtDynamic")).ToList();
int i = 1;
foreach (string key in keys)
{
this.CreateTextBox("txtDynamic" + i);
i++;
this.CreateTextBox("txtDynamic" + i);
i++;
}
}