2012-05-18 59 views
0

我有一箇中繼器,其中有一個按鈕用於保存一些數據到數據庫。我的問題是有時調用數據庫需要一點時間,用戶有時會點擊保存幾次,導致多個條目被添加到數據庫。所以我的第一個想法是添加一個throbber,並在點擊時禁用按鈕。跳動者正在旋轉,但是當按鈕被禁用時,它阻止了對服務器的呼叫。所以我看着this,但由於我有一堆不同的按鈕(更新,添加,...),有幾個不同的服務器端方法被調用,所以我不能只發布__doPostBack($(button).attr('id'),'')。 所以我想我可能需要做一個Ajax調用,但我想看看是否有任何其他想法。 這裏是我的代碼:onClientClick實際上是在服務器端安裝,但這基本上是做什麼。禁用按鈕可防止點擊事件到服務器

直放站

<div style="position: relative; float: left;"> 
     <asp:Button ID="btnSave" runat="server" Text="Assign" OnClientClick="return fnAssignedButtonPressed(this);", OnClick="btnSave_Click" /> 
</div> 

Javscript

function fnAssignedButtonPressed(button) { 
//validating inputs 
var valid = true; 

     if(valid) 
     { 
      $(button).attr('disabled', 'disabled'); 
      showWaitCursor(true); 
      __doPostBack($(button).attr('id'),''); 
     } 
    return valid; 
} 

Serverside集團

protected void btnSave_Click(object sender, EventArgs e) 
{ 
//save 
// This method doesn't call called when I disable the button!! 
} 

回答

0

我落得這樣做,但我不知道我的感受..

function fnAssignedButtonPressed(button) 
{ 
//validating inputs 
var valid = true; 
    if(valid) 
    { 
     showWaitCursor(true); 
     //set a timeout and then disable the button, this prevents the user from click the button multiple times. 
     // need the timeout bc when the button becomes disabled it prevents the onlick event from firing. 
     setTimeout(function() {fnDisableButton(button)}, 1); 
    } 
} 

function fnDisableButton(button) 
{ 
    $(button).attr('disabled', 'disabled'); 
} 
0

根據您的情況,Ajax絕對是一個不錯的選擇。否則,你可以使用jQuery的用戶界面對話框,如顯示請稍候模式:

$('<div class="dialog" title="Please Wait">Please wait while your request is processing</div>').dialog({ 
    modal: true, 
    width: 350, 
    open: function (event, ui) { 
     $(".ui-dialog-titlebar-close").hide(); 
    }, 
}); 

一旦服務器返回時,模式會自動消失,用戶界面對話框灰色的模式選項,在屏幕,使用戶可以不點擊任何東西。