简体   繁体   中英

Disable asp.net button on when clicked

I have the javascript code below which disables an asp.net button during page postback. I also have a dropdownlist whose Autopostback property is set to true. Unfortunately, when i change the selectedItem in the dropdownlist, the button is disabled.

I want the Button disabled only when the page postback is caused by that same button, but not any other control on the page.

Some one please help on what change i should make to the code below to achieve this.

<script type="text/javascript">
    // Get a reference to the PageRequestManager.
    var prm = Sys.WebForms.PageRequestManager.getInstance();

    // Using that prm reference, hook _initializeRequest
    // and _endRequest, to run our code at the begin and end
    // of any async postbacks that occur.
    prm.add_initializeRequest(InitializeRequest);
    prm.add_endRequest(EndRequest);

    // Executed anytime an async postback occurs.
    function InitializeRequest(sender, args) {

        // Change the Container div's CSS class to .Progress.
        $get('Container').className = 'Progress';

        // Get a reference to the element that raised the postback,
        //   and disables it.
        $get(args._postBackElement.id).disabled = true;
    }

    // Executed when the async postback completes.
    function EndRequest(sender, args) {
        // Change the Container div's class back to .Normal.
        $get('Container').className = 'Normal';

        // Get a reference to the element that raised the postback
        //   which is completing, and enable it.
        $get(sender._postBackSettings.sourceElement.id).disabled = false;
    }                 
</script>

Well, you're hooking into any asynchrous postback, sounds like you just want to hook into a particular button's postback.

However, you could fix this with minimal code adjustment, just by checking the ID of the control which triggered the postback before disabling it.

if (args._postBackElement.id == idOfTheButton)
   $get(args._postBackElement.id).disabled = true;

Of course, if your button is a server-side button (asp:button), then you would need to either render out the client-id to the client from the server, or access it directly:

if (args._postBackElement.id == '<%= YourButton.ClientId %>')
   $get(args._postBackElement.id).disabled = true;

As i said, there are better solutions than this (like only hooking into the postback event of the button, not any postback event).

But nonetheless, this should solve your problem.

You are referencing the id of the button here:

$get(args._postBackElement.id).disabled = true;

So all you need to do is to check the value of the ID prior to setting it to disabled:

if(args._postBackElement.id == '<%=MyButton.ClientID %>')
    $get(args._postBackElement.id).disabled = true;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM