1

I have a visual web part that has an update panel that has a button that performs an action. I want to prevent users from hitting that button more than once.

The solutions I found use javascript to disable the button then do the postback.

The problem with this that I am having is that my buttonclick method is getting called 2 times since it seems to go to Page_Load 2 times.

Here is my code. First the button and javascript then the Page_Load method.

 <script type="text/javascript">
    function DisableButton() {
        document.getElementById('<%= BetOnCar.ClientID %>').disabled = "disabled";
        __doPostBack('<%= BetOnCar.ClientID %>', ''); 

    } 
</script> 

<asp:Button ID="BetOnCar" runat="server" Text="Bet" OnClientClick="DisableButton();" onclick="BetOnCar_Click" Enabled="false" />

protected void Page_Load(object sender, EventArgs e)
    {
        string controlName = this.Page.Request["__EVENTTARGET"];
        if (BetOnCar.ClientID == this.Page.Request["__EVENTTARGET"])
        {
            BetOnCar_Click(sender, e);
        }
    }

I'm hoping that I am doing this type of thing in the wrong location because it seems like page_load gets hit more often than I expected.

2
  • Does this actually find your button: document.getElementById('<%= BetOnCar.ClientID %>')
    – Christophe
    Sep 7, 2012 at 17:41
  • It does get the button.
    – Josh
    Sep 7, 2012 at 18:27

2 Answers 2

1

I think you have add return false; in the javascript function DisableButton() OR use Jquery event.preventDefault() .

As the default button click is also firing

0

You'll get two post backs since you're adding a __doPostBack() in your DisableButton() function. Try to remove that one.

1
  • I tried that and I get only one postback but there is no way to know what to call from this as this.Page.Request["__EVENTTARGET"] is "" and it still won't directly go to the button click method
    – Josh
    Sep 7, 2012 at 18:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.