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.


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:

  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 post a comment on this article.

Doc ID 512
Full URL https://imar.spaanjaars.com/512/handling-disabled-links-in-aspnet-4
Short cut https://imar.spaanjaars.com/512/
Written by Imar Spaanjaars
Date Posted 12/09/2009 22:45


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 Doc ID of the document.

(Plain text only; no HTML or code that looks like HTML or XML. In other words, don't use < and >. Also no links allowed.