I have a disabled asp.Button, which I enable later with JavaScript. Like this
<asp:Button ID="btnSave" runat="server" Text="Save" Enabled="false" OnClientClick="; return ValidateFields();" OnClick="btnSave_Clicked" />
However, the "onclick" method is not rendered as html when the control is disabled. My work around is to add the following code in PageLoad.
btnSave.Attributes["onclick"] = "return ValidateFields();";
Is there a more convenient work around for this?
Thanks
You can use the html attribute for disabled
<asp:Button disabled="disabled" ID="btnSave" runat="server" Text="Save" Enabled="false" OnClientClick="; return ValidateFields();" OnClick="btnSave_Clicked" />
I assume you then make it enabled in clientside? if so then you can enable it with :
document.getElementById('MainContent_btnSave').removeAttribute('disabled'); //or jquery alternative where MainContent_btnSave is the clientid of the control
Well, it's obvious why the framework behaves like this: if a button is disabled, why it should have an onclick
event handler? The user can't click it if it's disabled! Because of this, the framework removes the attribute.
So, I don't think you have other options besides appending the attribute manually.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With