简体   繁体   中英

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. 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. 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.

Merely having the disabled attribute present means it is disabled. Try this:

btnLoad.removeAttribute("disabled");

Disable viewstate for that control and see if it works. The viewstate 'remembers' the button was disabled and probably doesn't fire on_click event

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. If you are doing it via javascript then do it in $(document).ready() .

Failing that you can override the onClick of the button and force the postback using JS:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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