Several controls were found with the same ID

I have the following asp.net code, but it gives an error when the dropdown index changes:

<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 to create the textbox and get the value of the textbox in the lable as follows

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);
    }



}

      

mistake:

Multiple controls with the same ID 'txtbox0' were found. FindControl requires that controls have unique IDs.

      

+3


source to share


5 answers


GUID

Change the following line

txtTextbox.ID = "txtbox" + sequentialId;

      

to line



txtTextbox.ID = "txtbox" + Guid.NewGuid().ToString("N");

      


You can use the same approach elsewhere.

+7


source


I found the following fix for the error you received.

When drp_SelectedIndexChanged

launched on SelectedIndexChanged

, and this is not zero index

, you get the error "Several controls were found with the same ID 'txtbox0'. FindControl requires controls to have unique IDs." when index 1 is selected more than once.

Change



if (drp.SelectedIndex != 0) statement 

      

For

if (drp.SelectedIndex != 0 && ViewState["controls"] != null)

      

+2


source


This error occurs when any control ID is missing, or more than one control has the same ID. to get rid of this error, make the identification values ​​of each control unique.

see example below, loop increment value j is added dynamically to id value to make id value increment

pnl.ID = "pnltype" + j.ToString ();

for (int j = 0; j < dtRtype.Rows.Count; j++)
{        
  pnl = new Panel();
  pnl.Width = panelWidth;
  pnl.Height = panelHeight;
  pnl.BackColor = Color.BlueViolet;
  pnl.BorderStyle = BorderStyle.Solid;
  pnl.BorderColor = System.Drawing.Color.White;
  pnl.BorderWidth = 2;
  pnl.Style["position"] = "absolute";
  pnl.Style["bottom"] = bottom.ToString() + "px";
  pnl.Style["left"] = left.ToString() + "px";
  pnl.ID = "pnltype" + j.ToString();

}

      

+1


source


Perhaps you can change the CreateTextBox procedure like this:

private void CreateTextbox(int Number)
{
  pnl.Controls.Clear();

      

0


source


The specific reason you are getting this error is that you are calling CreateTextbox()

in SelectedIndexChanged

when text fields have already been created on Page_Load

.

Answer to

@Pankaj works great, but I thought I'd add another solution given the cause of the error.

I understand that you need to CreateTextbox()

call on Page_Load

for the initial view, so ...

Decision

If you just added pnl.Controls.Clear()

to your method CreateTextbox()

before creating the textboxes again this will solve your problem. So it will look like this:

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;
      //Clear the controls before adding them again.
      pnl.Controls.Clear();
      pnl.Controls.Add(txtTextbox);
      pnl.Controls.Add(lbltxtTextbox);
    }
  }
  catch (Exception ex)
  {
  }
}

      

One of the reasons to consider this method when generating a random unique identifier is that sometimes you need to be able to predict the name of the control from the code behind so that you can get the value as I did.

Anyway, I thought I'd contribute if maybe someone needs a different approach. =)

-1


source







All Articles