I have following asp.net code but it gives error when I change dropdown selected index:
<asp:UpdatePanel>
<ContentTemplate>
<asp:DropDownList ID="drp" runat="server" AutoPostBack="true" OnSelectedIndexChanged="drp_SelectedIndexChanged">
<asp:ListItem Text="ABC" Value="ABC"></asp:ListItem>
<asp:ListItem Text="DEF" Value="DEF"></asp:ListItem>
</asp:DropDownList>
<asp:Panel ID="pnl" runat="server">
</asp:Panel>
<asp:Button ID="Button1" runat="server" Text="Submit" OnClick="Button1_Click" />
</ContentTemplate>
<Triggers>
<asp:PostBackTrigger ControlID="drp" />
<asp:PostBackTrigger ControlID="Button1" />
</Triggers>
</asp:UpdatePanel>
Now made one function for create textbox and get textbox value into lable as following way
protected void drp_SelectedIndexChanged(object sender, EventArgs e)
{
if (drp.SelectedIndex != 0)
{
ViewState["controls"] = true;
CreateTextbox(drp.SelectedIndex);
}
}
private void CreateTextbox(int Number)
{
try
{
TextBox txtTextbox;
Label lbltxtTextbox;
for (int i = 0; i < Number; i++)
{
txtTextbox = new TextBox();
txtTextbox.ID = "txtbox" + i;
lbltxtTextbox = new Label();
lbltxtTextbox.ID = "lbltxtbox" + i;
pnl.Controls.Add(txtTextbox);
pnl.Controls.Add(lbltxtTextbox);
}
}
catch (Exception ex)
{
}
}
private void GetTextboxvalue(int Number)
{
try
{
TextBox txtTextbox;
Label lbltxtTextbox;
for (int i = 0; i < Number; i++)
{
txtTextbox = (TextBox)pnl.FindControl("txtbox" + i);
lbltxtTextbox = (Label)pnl.FindControl("lbltxtbox" + i);
lbltxtTextbox.Text = txtTextbox.Text;
}
}
catch (Exception ex)
{
}
}
protected void Button1_Click(object sender, EventArgs e)
{
GetTextboxvalue(drp.SelectedIndex);
}
protected void Page_Load(object sender, EventArgs e)
{
if (ViewState["controls"] != null)
if (drp.SelectedIndex != 0)
{
CreateTextbox(drp.SelectedIndex);
}
}
error is:
Multiple controls with the same ID 'txtbox0' were found. FindControl requires that controls have unique IDs.
GUID
Change the following line
to below line
You can follow the same approach in remaining places.