asp.net OnClientClick未针对最初禁用的Button进行渲染

art*_*nce 16 javascript c# asp.net

我有一个禁用的asp.Button,稍后我用JavaScript启用它.像这样

<asp:Button ID="btnSave" runat="server" Text="Save" Enabled="false" OnClientClick="; return ValidateFields();" OnClick="btnSave_Clicked" />
Run Code Online (Sandbox Code Playgroud)

但是,禁用控件时,"onclick"方法不会呈现为html.我的工作是在PageLoad中添加以下代码.

btnSave.Attributes["onclick"] = "return ValidateFields();";
Run Code Online (Sandbox Code Playgroud)

有更方便的解决方法吗?

谢谢

TBo*_*jnr 18

您可以将html属性用于禁用

<asp:Button disabled="disabled" ID="btnSave" runat="server" Text="Save" Enabled="false" OnClientClick="; return ValidateFields();" OnClick="btnSave_Clicked" />
Run Code Online (Sandbox Code Playgroud)

我假设你然后在客户端启用它?如果是,那么您可以启用它:

document.getElementById('MainContent_btnSave').removeAttribute('disabled'); //or jquery alternative where MainContent_btnSave is the clientid of the control
Run Code Online (Sandbox Code Playgroud)