views:

54

answers:

2

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 ?

+1  A: 

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>
Aristos
that would fire on every Async postback vent on page ? Is there a way to just fire it on a certain button click event inside a certain update panel ?
Popo
@Popo you can filter by reading the "sender" argument and open it only when some of your panels are updating. Use this code, make some debug and see what message you get there.
Aristos
thanks Aristos.
Popo
+1  A: 

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

Mouhannad
thanks a lot, I figured it out finally. What does arg.set_cancel(true); did in your code ?
Popo
Also, if I use postBackElement.disabled=true in end request. It does not unblock it prperly. Do we have to use document.getElementById() instead ?
Popo
which browser and os are you using?
Mouhannad
"set_cancel" is meant to stop the ajax request (check http://msdn.microsoft.com/en-us/library/bb386518.aspx). isn't working for you?
Mouhannad
Is there a way to delay the disabled button period by say 2 seconds. I tried to wrap it up in setTimeout but its not working as expected.I am using Windows.
Popo
It's difficult to know why that wouldn't work without seeing any code. setTimeout should work fine depending of how you have used it
Mouhannad