Alex
Alex

Reputation: 3968

Asp.net runat="server" changes id

I have the following html, which I need to runat="server"

<h2 class="admin-manager-table-header" runat="server" id="hdrBorrower">Borrower</h2>

I then have the following CSS:

.admin-manager-table-header#headerPending, .admin-manager-table-header#hdrBorrower {
    background-color: rgb(0, 176, 240);
}

Which no longer works as the server has changed the id to:

ctl00_body_hdrBorrower

So my CSS is now

.admin-manager-table-header#headerPending, .admin-manager-table-header#hdrBorrower , .admin-manager-table-header#ctl00_body_hdrBorrower{
    background-color: rgb(0, 176, 240);
}

My question is two fold:

1.Will the server always change the id to this, or will it sometimes vary the ID?

2.If it is consistent in its naming of the ID, is it bad practice of me to use this ID in a CSS style sheet?

Upvotes: 0

Views: 182

Answers (1)

svidgen
svidgen

Reputation: 14302

Normally, it's subject to change based on the location of the node within the DOM (as seen by the server before responding to the client). There are two slightly different algorithms .NET can use for generating the ID. See Control.ClientIDMode.

As of .NET 4.0, I believe you can make your hand-picked ID "stick" using a ClientIdMode attribute of Static.

Upvotes: 2

Related Questions