Manish Pansiniya's Blog

.NET, C#, Javascript, ASP.NET and lots more…:)

Resolved : Multiple controls with the same ID were found. FindControl requires that controls have unique IDs.

with 5 comments

One of the reason to get the above error is logical implementation of dynamic control naming. Sometimes, we could not think of the future while writing the code. And might be that creates this error.

Following is the code where I get the error:

Label lblShoppingCartItem = new Label()
lblShoppingCartItem.ID = "lblShoppingCartItem” + CategoryID + ItemID;

Now, in above example following case will create problem

CategoryID ItemID ID of control
1 19 lblShoppingCartItem119
11 9 lblShoppingCartItem119

Whenever this two row will be in list to display, it throws this exception. So to resolve this we should try to put delimiter between two IDs.

So, it would be resolved once you put

lblShoppingCartItem.ID = "lblShoppingCartItem” + CategoryID + “_” + ItemID;

Advertisements

Written by Manish

May 31, 2010 at 2:34 pm

5 Responses

Subscribe to comments with RSS.

  1. Thanks Dude.

    I liked your idea of placing the delimiter between those two IDs.

    Otherwise I could have gone with some other difficult idea like appending a Guid etc. to the ID.

    Thanks for the article.

    Balaji Birajdar

    September 7, 2010 at 11:33 am

    • If you have IDs which create unique combination then its okay to use delimiter based combination.

      In GUID also, It might be created again and again for control. As well as when you want to receive those things in postback, it would be hard to manage.

      You can somehow find the primary key ID combination and use it on your page.

      Manish

      September 7, 2010 at 12:40 pm

  2. I already have a delimited but I still get that error.
    I checked the source code of the web page and didn’t find any two controls with the same id.
    I don’t know how to fix this issue now.
    Please help!

    anonymouse

    October 27, 2010 at 8:31 pm

    • lblShoppingCartItem.ID = “lblShoppingCartItem” + CategoryID + “_” + ItemID;

      aathi

      August 27, 2013 at 12:11 pm

  3. I was building my gridview dynamically behind the scene in a try-catch with my gridview in a loop in final. I’ve attached the counter on my ID declaration and solved my issue. Thank you!

    Gridview.ID = “GV” + i; //where ‘i’ is my int counter 😉

    Daimos

    October 3, 2012 at 9:10 pm


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: