开发者

Preventing rapid submission button clicks?

I found a solution like this but my onclick event is already tied to a code-behind handler:

MyButton.Attributes.Add("onclick", "this.disabled=true;" + Page.ClientScript.GetPostBackEventReference(MyButton, "").ToString());

onclick="this.disabled=true;__doPostBack('MyContrl$MyButton','');"

My code:

<asp:imagebutton id="CheckoutBtn" runat="server" ImageURL="Styles/Images/submit.gif" on开发者_JAVA技巧click="CheckoutBtn_Click">

code-behind:

protected void CheckoutBtn_Click(object sender, ImageClickEventArgs e)
        {
            {

                MyShoppingCart usersShoppingCart = new MyShoppingCart();
                if (usersShoppingCart.SubmitOrder(User.Identity.Name) == true)
                {
                    CheckOutHeader.InnerText = "Thank you.";
                    Message.Visible = false;
                    CheckoutBtn.Visible = false;
                }
                else
                {
                    CheckOutHeader.InnerText = "Submission Failed - Please try again. ";
                }
            }

        }


Disabling the Button serverside won't work, the Button will be disabled AFTER the PostBack, in this time the user can still click several times, disabling it in JavaScript this.disabled=true; is the only way to successfully do this.


Cases where you are trying to prevent the user from submitting a form multiple times are best handled using the Post/Redirect/Get pattern.

It is a very simple pattern and Wikipedia does a good job of explaining it: http://en.wikipedia.org/wiki/Post/Redirect/Get


I assume you want to skip clicks that come within a certain TimeSpan of previous clicks. Create a class variable "DataTime LastClickTime", initially set to DateTime.MinValue. When you enter the click handler, check if DateTime.Now - LastClickTime > TimeSpan(...desired...) and if it isn't, exit the click handler with a return.


Try to disable the button with javascript instead of disabling it server side?

<asp:imagebutton id="CheckoutBtn" runat="server" ImageURL="Styles/Images/submit.gif" onclick="CheckoutBtn_Click" OnClientClick="this.disabled=true;">


One way is hide the button from Javascript and show some ajax loader image.

function btnClientClick()
{
   document.getElementById('CheckoutBtn').style.display = 'none';
   document.getElementById('dvLoader').style.display = '';
}
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜