开发者

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.


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/


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜