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

一笑奈何 提交于 2019-12-12 11:07:31

问题


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.


回答1:


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/




回答2:


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.




回答3:


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.



来源:https://stackoverflow.com/questions/2153586/disable-an-asp-net-dynamic-button-click-event-during-postback-and-enable-it-afte

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!