Question

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?

Was it helpful?

Solution 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;
}

OTHER TIPS

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.

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?

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top