Help Multiple controls with the same ID '' were found. FindControl requires that controls have unique IDs
i keep getting this and nothin is helpful can some one solve this error i am addinfg images at runtime and links are fetched from db and dynamic image control are made but this error is getting in my way. Multiple controls with the same ID 'projectimg8' were found. FindControl requires that controls have unique IDs.
string get = ListBox1.SelectedItem.Text;
DataSet ds = con.getprojectgallery(get);
if (ds!=null)
{
int count = ds.Tables[0].Rows.Count;
for (int i = 0; i < count; i++)
开发者_运维百科 {
Image img = new Image();
img.ID = "projectimg" + count.ToString();
img.ImageUrl = ds.Tables[0].Rows[0][0].ToString();
img.Height = 80;
img.Width = 80;
img.ToolTip = ds.Tables[0].Rows[0][1].ToString();
pnlgallery.Controls.Add(img);
pnlgallery.Controls.Add(new LiteralControl("<br />"));
}
Change it to:
img.ID = "projectimg" + i.ToString();
You need the incremented count, not the count variable itself.
Make sure when you generate your images they all have unique IDs.
It would be easier of you showed us some code..
Edit:
Thanks for the code.
In the line where you set ID you use same number every time because count
represents the number of rows.
You could use Matt's code, or:
img.ID = String.Format("projectimg{0}", i);
精彩评论