前言
因為工作上的需求,需要簡單維護舊有的 VB.NET Web Application,無意間客戶發現表單按下送出按鈕後,在處理等待過程中,可以繼續按下送出按鈕,導致表單重覆送出事情發生。雖然很直覺地想到使用 JavaScript 或 JQuery 來解決這個問題,但實在不好處理。最後找到了合適的解決方法,在此紀錄並提供有需要的朋友參考。問題說明
當使用者點選送出按鈕的時候,網頁正在處理中,這時候卻可以連續點送出按鈕,導致表單送出數次。解決方法
原來的按鈕元件語法:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<asp:Button id="_btnConfirm" CssClass="btn" runat="server" Text="確定"></asp:Button> |
我們加上下列兩個屬性,即可以不讓使用者在處理期間,仍可以繼續按下送出按鈕
OnClientClick="this.disabled = true; this.value = 'Saving...';"
UseSubmitBehavior="false"
修改後如下
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<asp:Button id="_btnConfirm" CssClass="btn" runat="server" Text="確定" OnClientClick="this.disabled = true; this.value = 'Saving...';" UseSubmitBehavior="false"></asp:Button> |
接下來我們進行測試,你會發現按下按鈕後,按鈕文字變成 Saving,且無法重覆點選。大功告成
0 留言