views:

1239

answers:

4

Hello all,

I am creating a button dynamically in my code and attaching a click event to it. However I have to prevent people to click it while there is a process going on. So when it is clicked once, it should be disabled and when the process ends it should be enabled. How can I do that?

Thanks.

+2  A: 

onclick="this.enabled=false" add this from your code behind to your control

btnAdd.Attributes.Add("onclick", "this.enabled=false;");

This link explains in detail http://encosia.com/2007/04/17/disable-a-button-control-during-postback/

Ravia
if you mean mybutton.onlick = "this.enabled=false" then there is no onlick method defined for it.
stckvrflw
it would be client side event fired after the button is clicked, if you want to disable from server side it is easy btnAdd.Enabled = false;
Ravia
A: 

I would use an UpdatePanel arround the button. On click you can serverside disable the button. Use a trigger on the updatepanel that looks every x seconds if your extern process has returned a result. And I would advise you to source out long running processes into a windows service.

citronas
+1  A: 

Hi. If you are processing via ajax when the button is clicked- 1. Disable the button when processing starts 2. Enable the button after processing completes

If the button postbacks, the best way is to disable the button when it is clicked via javascript [I won't suggest jquery just for this particular task]. Since after postback the button will be enabled as it was earlier, you don't need to worry about enabling.

    <asp:Button ID="btn" runat="server" OnClientClick="disable(this)"
Text="Click me!" OnClick="btn_Click" />

 <script type="text/javascript">
        function disable(control)
        {
            control.disabled="disabled";
            //added alert to give the snapshot of what happens
            //when the button is clicked
            alert(100);
        }
 </script>

Hope this helps.

sangam
and how will it re enable itself after postback ends ?
stckvrflw
I write my code similar to yours. Everything is same but now OnClick="btn_Click" is not firing. If I remove OnClientClick ="disable(this)" then OnClick is firing. What do you think ?
stckvrflw
A: 

This is exact solution for you. The best option is, first check all the validations passed [If you have any validation controls] and then disable the button and then send request to server. All these are in single solution.

Check it out. http://praveenbattula.blogspot.com/2010/01/disable-button-in-onclick-and-process.html

Rare Solutions