当前位置: 动力学知识库 > 问答 > 编程问答 >

asp.net - 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);
分享给朋友:
您可能感兴趣的文章:
随机阅读: