views:

1787

answers:

2

Why do ASP.NET LinkButton controls with OnClientClick attribute and disabled by setting Enabled="false" still render onclick event handler in HTML while Button controls don't?

It seems counter-intuitive. Since anchors can't really be disabled in browsers, it makes more sense not to attach an onclick event (and href attribute) if it has been set disabled on server-side.

+2  A: 

Well I would agree that it doesn't server much purpose, but without changing the way the linkbutton renders with one of the many methods built into asp.net there really isn't anything you can do about it. Unless you want to conditionally handle clicks in clientside code and check element attributes. This is just the way it is currently implemented so when you need the functionality of a button that can be disabled it is best to stay way from linkbuttons or anchors entirely.

Quintin Robinson
A: 

This really has little to do with asp.net.

A hyperlink button still fires the onclick event even when disabled. Bottom line: baked into HTML. (An input tag, when disabled, does not fire.)

 <a href="javascript:alert('Hello!');" disabled="disabled">Click Me!</a>
Andrew Robinson
My point exactly. So why render the href/onclick attributes? If you see disabled Button controls rendered by asp.net, they are set to disabled AND onclick is not rendered (I guess for security purpose).
Chetan Sastry
input button doesn't fire onclick event when disabled.
Chetan Sastry
Maybe we are saying the same thing but an input tag / button does not fire the onclick event when disabled (event when when the markup is included.) Maybe asp.net is just optimizing things a bit since the onclick markup is not used / needed.
Andrew Robinson