Solved

Disable Button in AJAX Panel

Posted on 2010-08-30
5
683 Views
Last Modified: 2012-05-10
I have the following button that I would like to disable when it is clicked:
<asp:UpdatePanel id="uPnlC_Step42_UploadRules" runat="server">
<contenttemplate>
    <asp:Button ID="btn_Step42_UploadRules" runat="server" Text="Upload" onclick="btn_Step42_UploadRules_Click" />
</contenttemplate>
<triggers>
        <asp:PostBackTrigger ControlID="btn_Step42_UploadRules" />                
</triggers>
</asp:UpdatePanel> 

Open in new window


I'm able to disable all other buttons in AJAX panels with the below script.  However, for the button in the code above, it doesn't work (I assume because of the use of the trigger?)  This button controls a fileupload control which is why I require a postback.  This is the code I use to disable other AJAX buttons (works for all buttons except the one shown above):

<script language="javascript" type="text/javascript">
    var Page;
    var postBackElement;
    function pageLoad() {
        Page = Sys.WebForms.PageRequestManager.getInstance();
        Page.add_beginRequest(OnBeginRequest);
        Page.add_endRequest(endRequest);
    }

    function OnBeginRequest(sender, args) {
        postBackElement = args.get_postBackElement();
        postBackElement.disabled = true;
    }

    function endRequest(sender, args) {
        postBackElement.disabled = false;
    }  
</script>

Open in new window


Any idea how to make my button disable immediately when clicked?
0
Comment
Question by:Carleton_H
  • 3
  • 2
5 Comments
 
LVL 6

Expert Comment

by:thaytu888888
ID: 33560187
- <asp:PostBackTrigger ControlID="btn_Step42_UploadRules" /> this make the Upload button cause a full post back to the server, so the script pageLoad() which call OnBeginRequest and endRequest will never fired. You just need to add a small javascript to disable Upload button when clicked.
<asp:Button ID="btn_Step42_UploadRules" runat="server" Text="Upload" onclick="btn_Step42_UploadRules_Click" OnClientClick="disableButton(this);" />

//Javascripts
function disableButton(btn)
    {
        if(btn)
        {
            btn.disabled = true;
        }
    }

Open in new window

0
 

Author Comment

by:Carleton_H
ID: 33562499
I played around with your solution.  It works great at disabling the button, but unfortunately, the onclick="btn_Step42_UploadRules_Click" isn't getting fired when I have the OnClientClick="disableButton(this);" in there as well.  Any ideas?
0
 

Author Comment

by:Carleton_H
ID: 33562566
Is it possible to execute the btn_Step42_UploadRules_Click code-behind from the Javascript function you supplied?
0
 
LVL 6

Accepted Solution

by:
thaytu888888 earned 500 total points
ID: 33563950
- Here is the solution:
function disableButton(btn) {
            if (btn) {
                __doPostBack(btn.id, "");
                btn.disabled = true;
            }
        }
0
 

Author Comment

by:Carleton_H
ID: 33564218
Thanks, worked like a charm!
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

861 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question