DotnetSparrow
DotnetSparrow

Reputation: 27996

Multiple controls with the same ID were found

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.

Upvotes: 3

Views: 35236

Answers (5)

Hawkeye
Hawkeye

Reputation: 638

The specific reason you're getting this error is you are calling CreateTextbox() on SelectedIndexChanged when the textboxes have already been created on Page_Load.

@Pankaj's answer works great, but I thought I would add another solution, taking in consideration the reason for the error.

I understand you need CreateTextBox() to be called on Page_Load for the initial view, so...

SOLUTION

If you just add pnl.Controls.Clear() in your CreateTextBox() method before creating the textboxes again, that would resolve your issue. So it would 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 reason to consider this method over creating random unique ID's is: sometimes you need to be able to predict the name of the control from the code behind so you're able to retrieve a value, as I've had occasion.

Anyway, I thought I'd contribute, if perhaps someone needed a different approach. =)

Upvotes: -1

anandd360
anandd360

Reputation: 306

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

see the example below, the loop increment value j is appended dynamically to the id value to make the 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();

}

Upvotes: 1

Stef
Stef

Reputation: 1

Maybe you could change the CreateTextBox procedure as follows:

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

Upvotes: 0

Adil
Adil

Reputation: 148150

I have found the following fix for the error you got.

When drp_SelectedIndexChanged is fired on SelectedIndexChanged and it is not zero index you get the error "Multiple controls with the same ID 'txtbox0' were found. FindControl requires that controls have unique IDs." when index 1 is selected for more then one times.

Change

if (drp.SelectedIndex != 0) statement 

To

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

Upvotes: 2

Pankaj
Pankaj

Reputation: 10115

GUID

Change the following line

txtTextbox.ID = "txtbox" + sequentialId;

to below line

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

You can follow the same approach in remaining places.

Upvotes: 9

Related Questions