Disable button in update panel on async postback

2020-02-07 04:15发布

I have multiple update panels with various asp buttons on a single page. I want to disable the buttons which caused the postback in update panel untill it completes.

Is there a way to avoid using a third party control for this? through JQuery or any other method ?

2条回答
你好瞎i
2楼-- · 2020-02-07 05:03

You can use the start and stop message of the update panel to disable your controls. For example

<script type="text/javascript"> 
var prm = Sys.WebForms.PageRequestManager.getInstance();    
prm.add_initializeRequest(InitializeRequest);
prm.add_endRequest(EndRequest);

function InitializeRequest(sender, args) {
   document.getElementById("ButtonToDisable").disabled = true;
}

function EndRequest(sender, args) {
   document.getElementById("ButtonToDisable").disabled = false;
}
</script>
查看更多
狗以群分
3楼-- · 2020-02-07 05:13

You can either do this:

cs

//in pageload
//the request is not in postback or async mode
bt1.OnClientClick = "this.disabled = true; " + ClientScript.GetPostBackEventReference(bt1, null) + ";");

Note: you can replace "this.disabled = true" with a js function that will have better handling for disabling the button and maybe display a friendly message as well.


Or this:

http://msdn.microsoft.com/en-us/library/bb383989.aspx

js

Sys.WebForms.PageRequestManager.getInstance().add_initializeRequest(CheckStatus);
function CheckStatus(sender, arg)
{
   var postBackElement = arg.get_postBackElement();
   var prm = Sys.WebForms.PageRequestManager.getInstance();
   if (prm.get_isInAsyncPostBack() && postBackElement.id == "btn1") {
      arg.set_cancel(true);
      //display friendly message, etc
   }
}

Note: I modified it so it checks for the button's id. Replace "btn1"

Good luck!!

查看更多
登录 后发表回答