简体   繁体   English

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

[英]asp.net OnClientClick not rendered for initially disabled Button

I have a disabled asp.Button, which I enable later with JavaScript. 我有一个禁用的asp.Button,稍后我用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. 但是,禁用控件时,“onclick”方法不会呈现为html。 My work around is to add the following code in PageLoad. 我的工作是在PageLoad中添加以下代码。

btnSave.Attributes["onclick"] = "return ValidateFields();";

Is there a more convenient work around for this? 有更方便的解决方法吗?

Thanks 谢谢

You can use the html attribute for disabled 您可以将html属性用于禁用

<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? 好吧,很明显为什么框架的行为如下:如果一个按钮被禁用,为什么它应该有一个onclick事件处理程序? 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. 所以,除了手动附加属性之外,我认为你没有其他选择。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM