I have a drop-down menu which allows user to choose the value and the number of textbox will appeared based on what they have chose. The code works however, when the user rechoose the value there is this error.
Multiple controls with the same ID 'tb_PersonnelInvolved1' were found. FindControl requires that controls have unique IDs.
How do i solve this so that the user can rechoose the number of textbox to display out.
<div class="dropdown_div_style">
<asp:DropDownList ID="LTA_involved" runat="server" CssClass="dropdown_style" AutoPostBack="true" OnSelectedIndexChanged="LTA_involved_SelectedIndexChanged">
<asp:ListItem Value="0"/>
<asp:ListItem Value="1"/>
<asp:ListItem Value="2"/>
<asp:ListItem Value="3"/>
<asp:ListItem Value="4"/>
<asp:ListItem Value="5"/>
</asp:DropDownList>
</div>
private List<string> TextBoxIdCollection
{
get {
var collection = ViewState["TextBoxIdCollection"] as List<string>;
return collection ?? new List<string>();
}
set { ViewState["TextBoxIdCollection"] = value; }
}
protected void Page_Load(object sender, EventArgs e)
{
var collection = new List<string>();
if (bool_ltaStatus == true)
{
ArrayList arr_personnelName = Report.GetPersonnelInvolved(int_reportID, LTA.Text);
LTA_involved.SelectedValue = arr_personnelName.Count.ToString();
for (int i = 0; i < arr_personnelName.Count; i++)
{
var textbox = new TextBox {
ID = "tb_PersonnelInvolved" + i,
Text = arr_personnelName[i].ToString(),
CssClass = "personnel_involved"
}; textbox.Attributes["placeholder"] = "Type the name here";
collection.Add(textbox.ID); //Collect these TextBox id
PlaceHolder1.Controls.Add(textbox);
arr_oldPersonnelNames.Add(arr_personnelName[i].ToString());
}
}
foreach (string str_textBoxID in TextBoxIdCollection) {
var textbox = new TextBox { ID = str_textBoxID };
PlaceHolder1.Controls.Add(textbox);
}
}
protected void LTA_involved_SelectedIndexChanged(object sender, EventArgs e)
{
var collection = new List<string>();
if (Int32.TryParse(LTA_involved.SelectedItem.Value, out int total)) {
for (int i = 1; i <= total; i++)
{
var textbox = new TextBox {
ID = "tb_PersonnelInvolved" + i,
CssClass = "personnel_involved"
}; textbox.Attributes["placeholder"] = "Type the name here";
collection.Add(textbox.ID); //Collect these TextBox id
PlaceHolder1.Controls.Add(textbox);
}
TextBoxIdCollection = collection;
}
}