簡體   English   中英

單擊時禁用asp.net按鈕

[英]Disable asp.net button on when clicked

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

我希望僅當頁面回發是由相同按鈕引起的,而不是頁面上的任何其他控件時,才禁用按鈕。

請有人幫助我對以下代碼進行哪些更改以實現此目的。

<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>

好吧,您正在陷入任何異步的回發,聽起來就像您只想陷入特定按鈕的回發。

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

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

當然,如果您的按鈕是服務器端按鈕(asp:button),則需要從服務器向客戶端呈現客戶端ID,或直接訪問它:

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

如我所說,還有比這更好的解決方案(例如僅掛接到按鈕的回發事件,而不是任何回發事件)。

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

您在此處引用按鈕的ID:

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

因此,您所需要做的就是在將ID設置為Disabled之前檢查ID的值:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM