简体   繁体   English

不能结合禁用按钮和隐藏代码功能(JavaScript +隐藏代码)

[英]Can't combine button-disable and code-behind function (JavaScript + code-behind)

When clicking on Insert -button, it does whatever it's suppose to do. 单击“ Insert按钮时,它会执行应做的任何事情。 But I don't want user to be able to click the button more than once and therefore I want to disable it once you press it. 但是我不希望用户能够多次单击按钮,因此我想在按下按钮后将其禁用。

<asp:Button ID="Insert" runat="server" Text="Send" OnClick="Insert_OnClick" 
  OnClientClick="this.disabled=true; showLoading(this);" />

When combining Insert_OnClick and " disable ", the function Insert_OnClick wont run, because it somehow disable the button first and therefore the code-behind function wont run for that reason. 当组合Insert_OnClick和“ disable ”,功能Insert_OnClick不会运行,因为它在某种程度上第一,因此禁用按钮的代码隐藏功能不会运行这个原因。

I also tried to disable to button itself in showLoading js-function, same behavior. 我也尝试禁用showLoading js函数中的按钮本身,同样的行为。

Any idea how to make the code-behind function run as the button get disabled? 有什么想法如何使禁用按钮的代码隐藏功能运行?

You need to disable it in code behind. 您需要在后面的代码中将其禁用。 Even if you did disable it after the click, the changes made with javascript would still be undone after PostBack. 即使您在单击后确实禁用了它,使用javascript所做的更改在PostBack之后仍将被撤消。

protected void Button1_Click(object sender, EventArgs e)
{
    Insert.Enabled = false;
}

Or set a timeout on the OnClientClick? 还是在OnClientClick上设置超时? Maybe that will work. 也许会起作用。

OnClientClick="setTimeout(function () { this.disabled=true; }, 10);"

If possible, I would change the Submit behavior. 如果可能,我将更改Submit行为。

the problem with that approach is that the front end code takes place before the back end submit and if you disable the button before the back end submit then it won't be able to call the back end function. 这种方法的问题在于,前端代码发生在后端提交之前,并且如果您在后端提交之前禁用了按钮,则它将无法调用后端功能。

Instead i would use different approaches, like consuming a webMethod or submiting the whole form . 相反,我将使用其他方法,例如使用webMethod或提交整个表单。

with the web method you would be able to play a little bit with the front end while the back end finishes processing 使用web方法,您可以在后端完成处理时在前端播放一些内容

With the whole submit you'll have to play with the page_load Event 在整个提交过程中,您将不得不使用page_load事件

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

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