Reputation: 5
I am having this error: Multiple controls with the same ID 'ddl_Weight' were found. Find-control requires that controls have unique IDs. thus i am not add to add more objective text-box, drop-down list. I have to add objectives, drop-down list for weightage and achieved.
private List<DropDownList> inputDropDownList;
private List<DropDownList> inputDropDownList2;
protected void btn_AddObjectives_Click(object sender, EventArgs e)
{
int rowCount = 0;
//initialize a session.
rowCount = Convert.ToInt32(Session["clicks"]);
rowCount++;
//In each button click save the numbers into the session.
Session["clicks"] = rowCount;
//Create the textboxes and labels each time the button is clicked.
for (int i = 0; i < rowCount; i++)
{
TextBox TxtBoxO = new TextBox();
TxtBoxO.TextMode = TextBoxMode.MultiLine;
DropDownList DDLW = new DropDownList();
DropDownList DDLA = new DropDownList();
inputDropDownList = new List<DropDownList>();
inputDropDownList2 = new List<DropDownList>();
Label lblO = new Label();
Label lblW = new Label();
Label lblA = new Label();
TxtBoxO.ID = "TextBoxO" + i.ToString();
DDLW.ID = "DDLW" + i.ToString();
DDLA.ID = "DDLA" + i.ToString();
inputDropDownList.Add(DDLW);
inputDropDownList2.Add(DDLA);
TxtBoxO.Width = 325;
DDLW.Height = 25;
DDLA.Height = 25;
DDLA.ID = "ddl_Achieved";
DDLA.Items.Add("Select");
DDLA.Items.Add("5");
DDLA.Items.Add("10");
DDLA.Items.Add("15");
DDLA.Items.Add("20");
DDLA.Items.Add("25");
DDLA.Items.Add("30");
DDLA.Items.Add("35");
DDLA.Items.Add("40");
DDLA.Items.Add("45");
DDLA.Items.Add("50");
DDLA.Items.Add("55");
DDLA.Items.Add("60");
DDLA.Items.Add("65");
DDLA.Items.Add("70");
DDLA.Items.Add("75");
DDLA.Items.Add("80");
DDLA.Items.Add("85");
DDLA.Items.Add("90");
DDLA.Items.Add("95");
DDLA.Items.Add("100");
DDLW.ID = "ddl_Weight";
DDLW.Items.Add("Select");
DDLW.Items.Add("5");
DDLW.Items.Add("10");
DDLW.Items.Add("15");
DDLW.Items.Add("20");
DDLW.Items.Add("25");
DDLW.Items.Add("30");
DDLW.Items.Add("35");
DDLW.Items.Add("40");
DDLW.Items.Add("45");
DDLW.Items.Add("50");
DDLW.Items.Add("55");
DDLW.Items.Add("60");
DDLW.Items.Add("65");
DDLW.Items.Add("70");
DDLW.Items.Add("75");
DDLW.Items.Add("80");
DDLW.Items.Add("85");
DDLW.Items.Add("90");
DDLW.Items.Add("95");
DDLW.Items.Add("100");
lblO.ID = "LabelO" + i.ToString();
lblW.Text = "LabelW" + i.ToString();
lblA.ID = "LabelA" + i.ToString();
lblO.Text = "Objective " + " " + (i + 1).ToString() + " : ";
lblW.Text = " Weightage" + " " + (i + 1).ToString() + " : ";
lblA.Text = " Achieved " + " " + (i + 1).ToString() + " : ";
//Add the labels and textboxes to the Panel.
Panel1.Controls.Add(lblO);
Panel1.Controls.Add(TxtBoxO);
Panel1.Controls.Add(lblW);
Panel1.Controls.Add(DDLW);
Panel1.Controls.Add(lblA);
Panel1.Controls.Add(DDLA);
}
Upvotes: 0
Views: 4868
Reputation: 219027
The error is pretty clear. You're adding multiple controls with the same ID, which is disallowed. Note that you're in a loop:
for (int i = 0; i < rowCount; i++)
We'll assume that rowCount
is greater than 1 and that the loop iterates more than once. Within that loop you create controls:
DropDownList DDLW = new DropDownList();
DropDownList DDLA = new DropDownList();
And you assign IDs to them:
DDLW.ID = "DDLW" + i.ToString();
DDLA.ID = "DDLA" + i.ToString();
These IDs are probably unique. But then you overwrite them with ones that are not unique:
DDLA.ID = "ddl_Achieved";
DDLW.ID = "ddl_Weight";
And then you add them to the page:
Panel1.Controls.Add(DDLW);
Panel1.Controls.Add(DDLA);
The purpose of an ID
, as its name suggests, is to uniquely identify an object. Just like with id
attributes in HTML, if you re-use the same one multiple times in a page then the behavior becomes undefined. The system (in this case wherever you're using FindControl()
) expects there to be 0 or 1 elements with any given ID
value. You've created more then 1, which is invalid.
You can probably just remove the lines where you set the non-unique IDs and keep the lines where you set the unique ones.
Upvotes: 2