Andrei Maieras
Andrei Maieras

Reputation: 706

Add to an aspx button an css id

Having the code below for a button, can I add an id to it so I can add CSS to the button?? Or I can put only a class??

<asp:Button ID="registerLink" runat="server" Text="Create Account">
                </asp:Button>

Upvotes: 1

Views: 667

Answers (1)

Dai
Dai

Reputation: 155250

In WebForms, the ID="" attribute of controls is transformed into something of the form ctl0__ctl1__registerLink (where ctl0 and ctl1 are the ID="" values of parent controls). This means the rendered id="" attribute is (generally) unpredictable and cannot be relied upon for styling or Javascript uses.

There are three possible solutions:

  1. Use ctrl.ClientId to get the final rendered id="" attribute value, this works when you want to reference the rendered HTML from a client script on the same page, however it isn't of much use for styling unless it's an inline <style> element.
  2. Use the clientIDMode setting to override how the id="" attribute is rendered. This requires ASP.NET 4.0 or later. You can set this in web.config, in your <%@ Page declaration, or on each element. Set it to Static so the value is verbatim (with exceptions).
  3. Implement your own Control Adapters that override how attributes render.
  4. Ditch WebForms and use ASP.NET MVC ;)

Upvotes: 4

Related Questions