ASP.NET 2.5 prefixing ctl00 and ASP.NET 4 not prefixing ctl00

Does anyone know why ASP.NET 4 has dropped the ctl00 prefix on ASP controls?

Is there a setting I have missed?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

In ASP.NET 4.0, they’ve introduced support for cleaner HTML syntax. You can read about it at Scott Gu’s blog. If you want the classic model for Client IDs, you can adjust your web.config:

<configuration>
    <system.web>
        <pages controlRenderingCompatibilityVersion="3.5" />

And that’ll make upgrading your application easier. You can change this per control (and per page) by using the Control.ClientIDMode property, which can also be set in the web config:
<configuration>
    <system.web>
        <pages clientIDMode="AutoID|Predictable|Static|Inherit" />

AutoID renders the controls with the classic ASP.NET 2.0 model.

Method 2

As far as i remember its up to ASP.NET to decide which prefix to use.
Its a bad practice to reference on the controls with hardcoded id value.

You should use ClientId property that will always generate you proper Id:

<td class="tmarg10" style="width: 150px">
    <label for="<%=txtName.ClientID %>">
        Name of the mall group :</label>
</td>
<td class="tmarg10">
    <asp:TextBox ID="txtName" runat="server" Columns="90" /> <br />
</td>

In the example above, its calculating proper ID of the textbox and putting it into label attribute. In this way you will no more worry about keeping the same id of the control.

Method 3

If you just want to add ‘ct100’ prefix to your control’s IDs, add Master page to your WebPage. But how said above, try to avoid using hardcode with controls ids in your sources and read Metthew’s comments for generation custom ID

Method 4

Not a setting, but a decision by MS to simplify the way IDs of controls within containers are transformed to the client.

Here is a blog post by Scott Guthrie explaining the changes. The reasoning behind it:

Clean, Standards-Based, CSS-Friendly Markup


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x