Handling Disabled Links in ASP.NET 4

In my (quite possibly very short) series on "things I discovered today in ASP.NET 4", today I am discussing disabling links in ASP.NET 4.

Introduction

To be honest, it's not something I discovered today as I read about it a some time ago in the What's new in VS 2010 Web Development Beta 1 and 2 documents. However, I ran into it today again when doing a chapter for my new book Beginning ASP.NET 4 in C# and VB.NET. One of the exercises explains how to disable the Delete link for items in a GridView that are constrained by a relationship in the database. In other words, make it impossible to delete items that still have associated records. The code in that exercise looks like this:

if (Convert.ToInt32(myDataRowView["NumberOfReviews"]) > 0)
{ LinkButton deleteButton = e.Row.FindControl("DeleteButton") as LinkButton; deleteButton.Enabled = false; }

In versions of ASP.NET prior to ASP.NET 4, this would generate markup like this:

<a disabled="disabled" id="ctl00_cpMainContent_GridView1_ctl09_DeleteButton">

Notice how the Enabled property has been transformed in a disabled="disabled" attribute. This in turn causes the link to appear as follows (compare it to the Edit link next to it that doesn't have the attribute applied)

Disabled Links Post .NET 4

The ASP.NET 4 Way

If you run the same code in an ASP.NET 4 web site (and have not set the controlRenderingCompatibilityVersion attribute on the <pages /> element in web.config to anything pre ASP.NET 4) you get the following:

Disabled Links in .NET 4

What's up with that? Is this a bug? Did someone at Microsoft loose her mind? Nope; it's part of a larger plan to generate markup closer to the official W3C standard. The disabled attribute is, according to the standard, only meant to be applied to form controls such as input. When you look at the source for the "disabled link", you now find something like the following:

<a class="aspNetDisabled" id="cpMainContent_GridView1_DeleteButton_4">

Besides the shorter auto client ID, you can see that the disabled attribute has been replaced with a class attribute set to aspNetDisabled. To make the link appear disabled you can give this class a different color like this:

.aspNetDisabled
{
  color: #ccc;
}

This doesn't render the link exactly as its truly disabled pre ASP.NET 4 brother, but it comes close. Still trying to figure out a way to make it look even more disabled. I wonder if filters could be an option....

If you don't like the aspNetDisabled class name, you can change it by setting the DisabledCssClass property which is now a part of the WebControl class.

Now, what I don't get is why nobody calls the W3C with a message like: "Hey, disabled is not part of the spec for non form controls. Can you add it there please?" That would be DRY; fix it in one place instead of a gazillion web pages out there ;-)


Where to Next?

Wonder where to go next? You can read existing comments below or you can post a comment yourself on this article .


Consider making a donation
Please consider making a donation using PayPal. Your donation helps me to pay the bills so I can keep running Imar.Spaanjaars.Com, providing fresh content as often as possible.



Feedback by Other Visitors of Imar.Spaanjaars.Com

On Tuesday, August 28, 2012 9:46:46 PM David Parkin said:
Add this to your web.config

in the pages section

clientIDMode="AutoID" controlRenderingCompatibilityVersion="3.5"

The clientIDMode="AutoID" clears issues w/ unique ids changing w/ 4.0 and controlRenderingCompatibilityVersion clears this issue.

On Wednesday, August 29, 2012 10:10:11 AM Imar Spaanjaars said:
Hi David,

yes, that would certainly work, but at a high cost. First of all, it renders invalid HTML, the sole reason this "feature" was introduced in ASP.NET 4.

Secondly, and much more importantly, now all controls render in the old mode. For some, such as the Menu this is really bad as they render old-style HTML.

Cheers,

Imar
On Wednesday, August 29, 2012 12:51:52 PM David Parkin said:
Imar,

Do you know, is it possible to set controlRenderingCompatibilityVersion="3.5" in web.config and have one iframe in the application set to 4.0?
On Wednesday, August 29, 2012 2:21:06 PM Imar Spaanjaars said:
Hi David,

Nope, I don't think you can do that, unless the iframe comes from a different application.

Cheers,

Imar

Talk Back! Comment on Imar.Spaanjaars.Com

I am interested in what you have to say about this article. Feel free to post any comments, remarks or questions you may have about this article. The Talk Back feature is not meant for technical questions that are not directly related to this article. So, a post like "Hey, can you tell me how I can upload files to a MySQL database in PHP?" is likely to be removed. Also spam and unrealistic job offers will be deleted immediately.

When you post a comment, you have to provide your name and the comment. Your e-mail address is optional and you only need to provide it if you want me to contact you. It will not be displayed along with your comment. I got sick and tired of the comment spam I was receiving, so I have protected this page with a simple calculation exercise. This means that if you want to leave a comment, you'll need to complete the calculation before you hit the Post Comment button.

If you want to object to a comment made by another visitor, be sure to contact me and I'll look into it ASAP. Don't forget to mention the page link, or the QuickDocId of the document.

For more information about the Talk Back feature, check out this news item.