如何为同一个按钮运行客户端和服务器端代码?

时间:2022-06-22 03:20:59

In Asp.NET aspx page, there is a button for data Save, when user click on this button, I want: 1. Disable this button immediately 2. Then call code behind function Save() to save data back to DB 3. Then enable this button again.

在Asp.NET aspx页面中,有一个数据保存按钮,当用户点击此按钮时,我想:1。立即禁用此按钮2.然后调用函数Save()后面的代码将数据保存回DB 3.然后再次启用此按钮。

What I did as below:

我做了如下:

  1. JS function in apsx:

    apsx中的JS函数:

    function DisableSave(){ saveButton = document.getElementById('<%=btnSave.ClientID%>'); saveButton.disabled=true; return false; }

    function DisableSave(){saveButton = document.getElementById('<%= btnSave.ClientID%>'); saveButton.disabled = TRUE;返回虚假; }

  2. Button script:

    按钮脚本:

    <---asp:Button ID="btnSave" OnCommand="Button_Command" CommandName="Save" runat="server" Text="Save" OnClientClick="javascript:DisableSave();"/>

    <--- asp:Button ID =“btnSave”OnCommand =“Button_Command”CommandName =“Save”runat =“server”Text =“Save”OnClientClick =“javascript:DisableSave();”/>

  3. In code behind function Save()(supposed called by CommandName), set Save button enable back.

    在函数Save()后面的代码中(假设由CommandName调用),设置Save按钮启用。

But When I run the code, only disable the save button, Code behind call did not happened.

但是当我运行代码时,只禁用保存按钮,没有发生代码隐藏调用。

How to fix it?

怎么解决?

2 个解决方案

#1


3  

Apparently some browsers (IE) won't process the submit action of a button if it's disabled. I tried this in Firefox and it did postback, but no luck with IE. One workaround, though not providing user feedback, would be to do OnClientClick="this.onclick= function() { return false; }". That will atleast prevent more postbacks from happening.

显然,如果某个浏览器(IE)被禁用,它将不会处理按钮的提交操作。我在Firefox中尝试了这个并且它确实发布了回复,但是IE没有运气。一种解决方法,虽然不提供用户反馈,但是要做OnClientClick =“this.onclick = function(){return false;}”。这至少会阻止更多的回发事件发生。

EDIT: onclick needed to be a function not a string.

编辑:onclick需要是一个函数而不是字符串。

#2


2  

The problem is that disabling the button using JavaScript in the onclick event stops the postback from occurring.

问题是在onclick事件中使用JavaScript禁用按钮会停止回发。

A simple workaround is illustrated in this CodeProject article.

此CodeProject文章中说明了一种简单的解决方法。

#1


3  

Apparently some browsers (IE) won't process the submit action of a button if it's disabled. I tried this in Firefox and it did postback, but no luck with IE. One workaround, though not providing user feedback, would be to do OnClientClick="this.onclick= function() { return false; }". That will atleast prevent more postbacks from happening.

显然,如果某个浏览器(IE)被禁用,它将不会处理按钮的提交操作。我在Firefox中尝试了这个并且它确实发布了回复,但是IE没有运气。一种解决方法,虽然不提供用户反馈,但是要做OnClientClick =“this.onclick = function(){return false;}”。这至少会阻止更多的回发事件发生。

EDIT: onclick needed to be a function not a string.

编辑:onclick需要是一个函数而不是字符串。

#2


2  

The problem is that disabling the button using JavaScript in the onclick event stops the postback from occurring.

问题是在onclick事件中使用JavaScript禁用按钮会停止回发。

A simple workaround is illustrated in this CodeProject article.

此CodeProject文章中说明了一种简单的解决方法。