stckvrflw
stckvrflw

Reputation: 1519

Disable an asp.net dynamic button click event during postback and enable it afterwards

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.

Upvotes: 5

Views: 14334

Answers (3)

Sangam Uprety
Sangam Uprety

Reputation: 1482

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.

Upvotes: 2

citronas
citronas

Reputation: 19365

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.

Upvotes: 0

Ravi Vanapalli
Ravi Vanapalli

Reputation: 9942

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/

Upvotes: 4

Related Questions