| Details | ![]() |
| QuickDocId | 512 |
| Written by | Imar Spaanjaars |
| Posted | 12/09/2009 22:45 |
| Page views | 1386 |
Are you looking to hire an experienced software developer or .NET consultant? Then get in touch with me through my company's web site at devierkoeden.com
Found an interesting article on this site? Got inspired by something you read here? Then consider making a donation with PayPal.
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)
![]()
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:
![]()
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 ;-)
Wonder where to go next?
You can post a comment 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.
| QuickDocId | 512 |
| Full URL | http://imar.spaanjaars.com/512/handling-disabled-links-in-aspnet-4 |
| Short cut | http://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 QuickDocId of the document.
For more information about the Talk Back feature, check out this news item.
Something went wrong while processing your comment. There's a fair chance things broke down because you tried to post something that looks like HTML in the comment field. Things that look HTML include (X)HTML, obviously, XML, ASP.NET markup and c# generics syntax as all of them use the < and > characters. If that's the case, modify your message and try again.
The number you entered is not correct. Please enter the sum of the two numbers again.