2010-08-30 106 views
2

我有下面的JavaScript代碼禁用頁面回發期間的asp.net按鈕。我也有一個其Autopostback屬性設置爲true的下拉列表。不幸的是,當我在下拉列表中更改selectedItem時,該按鈕被禁用。當點擊時禁用asp.net按鈕

我希望只有在頁面回發是由同一個按鈕引起,但沒有頁面上的任何其他控件導致按鈕禁用。

有人請幫助我應該對下面的代碼做出什麼樣的改變來實現這一點。

<script type="text/javascript"> 
    // Get a reference to the PageRequestManager. 
    var prm = Sys.WebForms.PageRequestManager.getInstance(); 

    // Using that prm reference, hook _initializeRequest 
    // and _endRequest, to run our code at the begin and end 
    // of any async postbacks that occur. 
    prm.add_initializeRequest(InitializeRequest); 
    prm.add_endRequest(EndRequest); 

    // Executed anytime an async postback occurs. 
    function InitializeRequest(sender, args) { 

     // Change the Container div's CSS class to .Progress. 
     $get('Container').className = 'Progress'; 

     // Get a reference to the element that raised the postback, 
     // and disables it. 
     $get(args._postBackElement.id).disabled = true; 
    } 

    // Executed when the async postback completes. 
    function EndRequest(sender, args) { 
     // Change the Container div's class back to .Normal. 
     $get('Container').className = 'Normal'; 

     // Get a reference to the element that raised the postback 
     // which is completing, and enable it. 
     $get(sender._postBackSettings.sourceElement.id).disabled = false; 
    }     
</script> 

回答

0

好吧,你鉤住任何 asynchrous回發,聽起來像你只是想掛到一個特定的按鈕的回發。

但是,您可以通過最小代碼調整來修復此問題,只需在禁用它之前檢查觸發回發的控件的ID即可。

if (args._postBackElement.id == idOfTheButton) 
    $get(args._postBackElement.id).disabled = true; 

當然,如果你的按鈕是一個服務器端的按鈕(ASP:按鈕),那麼你就需要或者從服務器呈現出客戶端識別到客戶端,或直接訪問它:

if (args._postBackElement.id == '<%= YourButton.ClientId %>') 
    $get(args._postBackElement.id).disabled = true; 

正如我所說的,還有比這更好的解決方案(如只掛鉤到按鈕的回發事件,沒有任何回發事件)。

但是,這應該可以解決您的問題。

0

您在這裏引用按鈕的ID:

$get(args._postBackElement.id).disabled = true; 

因此,所有你需要做的就是將ID值檢查其設置爲禁用之前:

if(args._postBackElement.id == '<%=MyButton.ClientID %>') 
    $get(args._postBackElement.id).disabled = true;