Click here to Skip to main content
15,892,643 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have added buttons and some others conrols dynamically as follows:

I want to handel events of button. Currently it gives me the error "Multiple Controls with same ID 'xx' found"..So how to deals with this situation...suppose i have create only one button then its event will be handel successfully. Problems occurs with multiple buttons...

for (int i = 0; i < dt1.Rows.Count-3; i++)
{
pnlTextBox = new Panel();
pnlTextBox.ID = "pnlTextBox" + i.ToString();
pnlTextBox.CssClass = "tagpanel";


TextBox txt = new TextBox();
txt.ID = (i + 1).ToString();
txt.CssClass = "txtb";
txt.Text = dt1.Rows[i][3].ToString();
txt.Enabled = false;
txt.AutoPostBack = true;

ImageButton img = new ImageButton();
img.ID = "img_" + i.ToString();
img.Click += new ImageClickEventHandler(img_Click);

img.ImageUrl = "~/images/TagClose.png";
img.CssClass = "icons";


Button Btn = new Button();
Btn.ID = "Btn_" + i.ToString();
Btn.Text = "Save";
Btn.CssClass = "btnCss";
Btn.Click += new EventHandler(Btn_test);

Literal ltbr1 = new Literal();
ltbr1.Text = "
";

Literal ltbr = new Literal();
ltbr.Text = "
";

Label lbl = new Label();
lbl.ID = i.ToString();
lbl.CssClass = "lblCss";
lbl.Text = dt1.Rows[i][0].ToString();


pnlTextBox.Controls.Add(txt);
pnlTextBox.Controls.Add(img);
pnlTextBox.Controls.Add(ltbr1);
pnlTextBox.Controls.Add(lbl);
}
Posted

1 solution

It seems you're setting Textbox IDs to i+1 and label controls to i. After second pass you'll have txt.ID = 1 and lbl.ID = 1. Add a prefix in front like you're doing with the button control.

If this helps please take time to accept the solution. Thank you.
 
Share this answer
 
Comments
Member 11151142 27-Jan-15 2:43am    
Yes...issue is fixed now.Silly mistake.Thank you
Sinisa Hajnal 27-Jan-15 3:17am    
Four eyes always better then two. :) Glad to help.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900