This post originated from an RSS feed registered with .NET Buzz
by Peter van Ooijen.
Original Post: Disable an asp.net LinkButton (or HyperLink) without graying it out
Feed Title: Peter's Gekko
Feed URL: /error.htm?aspxerrorpath=/blogs/peter.van.ooijen/rss.aspx
Feed Description: My weblog cotains tips tricks and opinions on ASP.NET, tablet PC's and tech in general.
I have a web form with a couple of linkbuttons. The linkbuttons serve as tab selectors, clicking one will activate an associated panel. When a panel is selected I don't want the user to select it again, clicking the linkbutton would be an unnecessary roundtrip. I can set the button's enabled property to false but the now the button will be grayed out. But this does not fit the user's impression of having precisely selected that tab.
Many asp.net controls have the AutoPostback property to disable an unintended postback. A linkbutton does not. But there is a very simple way to knock out postback with a snippet of script. This code does exactly what I want
It sets the clientside click handler to the JavaScript statement return false; Which cancels postback. The appearance of the linkbutton is changed by setting its CSS class, so the user is informed about the status of the button. Both the attributes and the cssclass are included in the linkbutton's viewstate. To restore the control's original state on the next roundtrip you need to reset it from code or disable the control's viewstate.
This also works for a hyperlink. But I do not see a meaningful use for it there. Yet.