2

Multiple controls with the same ID 'xxxx' were found. FindControl requires that controls have unique IDs.

I want to bypass this validation. I want to have same id's for multiple elements. I am creating dynamic controls and my css class is applied based on id and class combinations. Everything works when the page loads for the first time, but on page postback I get this error.

Can I do anything to bypass this validation?

Patrick
  • 17,669
  • 6
  • 70
  • 85
CıPhEr
  • 49
  • 1
  • 7
  • 1
    I think OP is looking for something akin to an html class. – spender Jun 07 '13 at 11:30
  • A tip: rename :) Use "FindControl" and want it to guess what the element is asking too much, unless the element has one feature in particular accessible – PiLHA Jun 07 '13 at 11:31
  • 1
    @user2463313 Are you really using ASP.NET 1.1? – Vitor Canova Jun 07 '13 at 11:35
  • Yes using 1.1 Guyz can anyone explain it to me why did it work on page load and failed on postback? – CıPhEr Jun 07 '13 at 11:38
  • Welcome to Stackoverflow! When asking questions, you don't have to add tags to the title, the tag system takes care of that. Please read http://meta.stackexchange.com/q/19190/147072 for more information. – Patrick Jun 07 '13 at 12:03
  • I am generating controls dynamically and this thing worked for me objButton.Attributes.Add("id", "duplicateID"); i can have as may as buttons with same id, this way. ASP.net does not bug me. :) This Works!!! So, thing is we can have multiple controls with same id's. Can anybody tell me one thng thoug, why does i work when i add attribut like this and it does not work when i do objButton.ID = "duplicateID" ???? – CıPhEr Jun 07 '13 at 12:54

4 Answers4

4

In HTML, you cannot have multiple elements with the same ID. Sounds more logical that you rewrite your CSS to not be based on ID.

sphair
  • 1,674
  • 15
  • 29
  • Ok, but I still recommend to rewrite the CSS. – sphair Jun 07 '13 at 11:32
  • @TimSchmelter can you please tell me why did i get the error on postback only? – CıPhEr Jun 07 '13 at 11:38
  • cannot and should not is not the same thing. :-) Most browsers display pages with duplicate id's on elements, so you definitely *can* have two controls with the same id. – Patrick Jun 07 '13 at 12:06
  • I am generating controls dynamically and this thing worked for me objButton.Attributes.Add("id", "duplicateID"); i can have as may as buttons with same id, this way. ASP.net does not bug me. :) – CıPhEr Jun 07 '13 at 12:52
2

You can't, ASP requires unique IDs. Otherwise how will know which control to reference in the code?

E.g

string x = NameBox.Text;

Well which one would you use if you had 20 of those, all with different content?

I would suggest you rewrite your CSS to not be based on IDs, maybe follow Harry Roberts way of Hashed Classes?

Ryan McDonough
  • 9,732
  • 3
  • 55
  • 76
  • Thanks for your reply, the funny thing is it worked during page load as i mentioned, but after postback it gave me this error. I am recreating controls after postback – CıPhEr Jun 07 '13 at 11:30
  • I am generating controls dynamically and this thing worked for me objButton.Attributes.Add("id", "duplicateID"); i can have as may as buttons with same id, this way. ASP.net does not bug me. :) – CıPhEr Jun 07 '13 at 12:52
2

You cannot have several elements with the same Server ID inside a common NamingContainer.
If you really want to have the same Server ID, put your controls inside separate NamingContainer (RepeaterItem?).

Now if you need the same Html/DOM ID, you can't have that.
Use something else than the Id to distinct your elements from a CSS point of view.
You could use partially matching ID though...

*[id*="_MyButtonID"].red {
    background-color: red;
}
Serge
  • 6,554
  • 5
  • 30
  • 56
  • Naming container. Are you saying this is allowed ?
    since id of both divs are different, table can have same ID ?
    – CıPhEr Jun 07 '13 at 11:39
  • 1
    No, that's not a naming container, a naming container is something ASP.Net handles. In generated html code, you can see naming container effect on your html elements ID (something like "ctrl001..._MyActualServerID") – Serge Jun 07 '13 at 11:44
  • I am generating controls dynamically and this thing worked for me objButton.Attributes.Add("id", "duplicateID"); i can have as may as buttons with same id, this way. ASP.net does not bug me. :) – CıPhEr Jun 07 '13 at 12:52
  • I am marking this as an answer, because this one was the closet to solve my problem. Thanks. – CıPhEr Jun 07 '13 at 13:00
1

Why don't use you give your controls multiple classes instead of relying on the IDs? Trying to use controls with the same IDs really isn't going to work.

For example:

<asp:TextBox id="textFirstName" runat="server" CssClass="name firstname" />
<asp:TextBox id="textLastName" runat="server" CssClass="name lastname" />

In your CSS you can then use selectors such as .name.firstname to select elements with BOTH the name and firstname class

Note - there's no space between the class names .name.firstname.

user2466466
  • 170
  • 6