简体   繁体   English

最初禁用的按钮在被Javascript启用后不会回发

[英]Initially disabled Button won't post back after it is enabled by Javascript

I have a button that is disabled by default, but gets enabled (or not disabled?) through JavaScript, but it's not actually doing anything (ie, posting) when I click it. 我有一个默认情况下处于禁用状态的按钮,但是可以通过JavaScript启用(或未禁用?),但是当我单击该按钮时,它实际上并未执行任何操作(即发布)。 Relevant code: 相关代码:

<asp:Button runat="server" ID="btnLoad" Text="Load" 
    OnClick="btnLoad_Click" Enabled="False"/>

Enabled through: 通过以下方式启用:

var btnLoad = $get("<%= btnLoad.ClientID %>");
btnLoad.disabled = false;

I hooked up another button to post back and enable the original button (btnLoad) on the server, and that seemed to do the trick. 我挂了另一个按钮以回发并在服务器上启用原始按钮(btnLoad),这似乎可以解决问题。 So it seems like it's something with the button being enabled on the client, but not on the server. 因此,似乎在客户端上启用了按钮,但在服务器上未启用按钮。 I suppose there's nothing I can do except force a post back to enable the button? 我想我除了强制发布帖子以启用按钮外什么也不能做? I'm not sure why I'm still posting this question now, but I will anyway :) 我不确定为什么我现在仍在发布此问题,但我还是会:)

It's because the server thinks "oh, that button is disabled, I'll ignore that message". 这是因为服务器认为“哦,该按钮被禁用,我将忽略该消息”。

The best way would be to enable it server-side, then disable it with JavaScript on the page (put a <script> tag at the top of the page) - and then later re-enable it as you do now. 最好的方法是在服务器端启用它,然后在页面上使用JavaScript禁用它(在页面顶部放一个<script>标记),然后像现在一样重新启用它。

Merely having the disabled attribute present means it is disabled. 仅存在disabled属性意味着它已被禁用。 Try this: 尝试这个:

btnLoad.removeAttribute("disabled");

Disable viewstate for that control and see if it works. 禁用该控件的viewstate并查看它是否有效。 The viewstate 'remembers' the button was disabled and probably doesn't fire on_click event viewstate“记住”按钮被禁用,可能不会触发on_click事件

I am assuming you disable the button before the event is registered with the server hence the reason it's not working when you enable it. 我假设您在事件注册到服务器之前禁用了该按钮,因此启用该按钮后该按钮不起作用的原因。

If you are disabling it in the code behind then do it on the Pre_Render event instead. 如果要在后面的代码中禁用它, Pre_RenderPre_Render事件上执行它。 If you are doing it via javascript then do it in $(document).ready() . 如果您是通过javascript执行的,则在$(document).ready()

Failing that you can override the onClick of the button and force the postback using JS: 未能覆盖按钮的onClick并使用JS强制回发:

$('#<%= btnLoad.ClientID %>').onClick(function() {
    __doPostBack("<%= btnLoad.ClientID %>", "");
});

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

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