Disabling a Button on the Client
When looking at developing with ASP.NET AJAX, you have to understand how server-side settings affect client-side settings. Let's take a look at the button below.
<asp:Button ID="btnSave" runat="server" UseSubmitBehavior="false" />
This button renders the following:
<input type="button" onclick="__doPostBack(..)" .. />
If I would have not specified submit behavior, it would have used type="submit". An input with type="button" actually works better with ASP.NET AJAX, because you can tap into the button events more easily.
I was reading "ASP.NET AJAX 3.5" from Wrox, and the authors had a good point; because of applications using more scripting, it may be beneficial to disable key features until all of the scripts have loaded. For instance, if you have validation that validates input on a form based upon a button click, and the user clicks the button before the page is used to it, then an exception could occur and the page wouldn't work as expected.
A better solution is to disable the button, so I tried this:
<asp:Button ID="btnSave" runat="server" UseSubmitBehavior="false" Enabled="false" />
But this dealt me a blow; it omitted the onclick call to __doPostBack, which is not what I wanted. Because Button inherits from WebControl, and WebControl implements IAttributeAccessor, this IAttributeAccessor specifies that any assignment that doesn't map to a server-side property automatically gets passed to the client. This means that if I did:
<asp:Button ID="btnSave" runat="server" UseSubmitBehavior="false" disabled="disabled" />
The button would render as:
<input type="button" onclick="__doPostBack(..)" disabled="disabled" />
This is because disabled isn't a server side property, and it automatically gets passed to the client. Getting back to the original point, the pageLoad() event fires on the client when the scripts have been loaded, so I can reenable the button as:
function pageLoad() {
$get("<%= btnSave.ClientID %>").disabled = false;
}
This feature only works if scripts work; if JS is disabled, then this isn't a good approach. Thanks to the Wrox authors for relaying that tip to me.