开发者

asp.net OnClientClick not rendered for initially disabled Button

开发者 https://www.devze.com 2023-03-06 03:28 出处:网络
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();

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 Page开发者_如何学PythonLoad.

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消