Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

asp.net OnClientClick not rendered for initially disabled Button

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

like image 826
artsince Avatar asked May 13 '11 09:05

artsince


2 Answers

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
like image 69
TBohnen.jnr Avatar answered Sep 23 '22 03:09

TBohnen.jnr


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.

like image 37
Albireo Avatar answered Sep 24 '22 03:09

Albireo