Reputation: 706
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
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:
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.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).Upvotes: 4