Solved

Webcontrol button click event being fired when browser Refresh button pressed

Posted on 2004-09-21
4
1,530 Views
Last Modified: 2010-05-18
Hi,

We have a WebControl button on a page that the user presses to go and do some stuff server side - this is fine but... after the user has pressed the Webcontrol button and then, for whatever reason, clicks IE5's Refresh button the webcontrol button click event gets fired again! - WHY?

Thanks.

James.
0
Comment
Question by:JAMES
  • 2
4 Comments
 
LVL 18

Accepted Solution

by:
DotNetLover_Baan earned 500 total points
Comment Utility
IE5's Refresh button is designed in a way that, it will perform the most recent POST BACK. Since "Submit" was the latest one, it will resubmit , untill you change it from the code. See this PAQ:
http://www.experts-exchange.com/Programming/Programming_Languages/Dot_Net/ASP_DOT_NET/Q_21129466.html

-Baan
0
 
LVL 17

Expert Comment

by:AerosSaga
Comment Utility
Here are some other options:

Private Sub DisableOnSubmitScript()

     Dim sScript As String = "<script language=""javascript"">" & Environment.NewLine
     sScript += ("<!--") & Environment.NewLine
     sScript += Environment.NewLine
     sScript += "var ctl_1;  // button control" & Environment.NewLine
     sScript += "var ctl_2;  // button control" & Environment.NewLine
     sScript += "var name_1 = '" & Me.Button1.UniqueID & "'" & Environment.NewLine
     sScript += "var name_2 = '" & Me.Label1.UniqueID & "'" & Environment.NewLine
     sScript += Environment.NewLine
     sScript += "if (document.all) {" & Environment.NewLine
     sScript += "   // IE browser" & Environment.NewLine
     sScript += "   ctl_1 = document.all[name_1];" & Environment.NewLine
     sScript += "   ctl_2 = document.all[name_2];" & Environment.NewLine
     sScript += "}" & Environment.NewLine
     sScript += "else if (document.layers) {" & Environment.NewLine
     sScript += "     // Netscape browser" & Environment.NewLine
     sScript += "     ctl_1 = document.layers[name_1];" & Environment.NewLine
     sScript += "     ctl_2 = document.layers[name_2];" & Environment.NewLine
     sScript += "}" & Environment.NewLine
     sScript += "else if (document.getElementById) {" & Environment.NewLine
     sScript += "     // Others" & Environment.NewLine
     sScript += "     ctl_1 = document.getElementById(name_1);" & Environment.NewLine
     sScript += "     ctl_2 = document.getElementById(name_2);" & Environment.NewLine
     sScript += "}" & Environment.NewLine
     sScript += Environment.NewLine
     sScript += "ctl_2.style.visibility = 'hidden'" & Environment.NewLine
     sScript += Environment.NewLine
     sScript += "function DisableOnSubmit() {" & Environment.NewLine
     sScript += "     if (ctl_1) {" & Environment.NewLine
     sScript += "          ctl_1.disabled = true;" & Environment.NewLine
     sScript += "     }" & Environment.NewLine
     sScript += "     if (ctl_2) {" & Environment.NewLine
     sScript += "          ctl_2.style.visibility = 'visible';" & Environment.NewLine
     sScript += "     }" & Environment.NewLine
     sScript += "     alert('okay!');     // this line should be removed later" & Environment.NewLine
     sScript += "     return true;" & Environment.NewLine
     sScript += "}" & Environment.NewLine
     sScript += Environment.NewLine
     sScript += "//-->" & Environment.NewLine
     sScript += "</script>"

     Me.RegisterStartupScript("_DisableOnSubmitScript", sScript)
     Me.RegisterOnSubmitStatement("_DisableOnSubmit", "DisableOnSubmit();")

End Sub


One work around to this problem is turn off the CausesValidation property of the button and handle the validation in the code behind btnSave_Click.

    Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
        RequiredFieldValidator1.Validate()
    End Sub

But this might be unwanted since the validation error message will not appear as normal. Another options is still to set the CausesValidation property to false. Then add this to code-behind page_load

btnSave.Attributes.Add("OnClick", "PageValidate();")

And add client code for the button, such as the following

function PageValidate()
{
     // show message here...

     // add validation code here...
     var isValid;
     if (typeof(Page_ClientValidate) == 'function') {

        Page_ClientValidate();
        isValid = Page_IsValid;
     }
     else  {
        // another validation code if necessary
        isValid = true;
     }

     // maybe more code here, after validation

     return MyResult;
}


Sorry, need to fix this line.

return isValid;

 Here's the VB code (translated from above code)

Dim sb As New System.Text.StringBuilder

sb.Append("if (typeof(Page_ClientValidate) == 'function') { ")
sb.Append("if (Page_ClientValidate() == false) { return false; }} ")
sb.Append("this.value = 'Please wait...';")
sb.Append("this.disabled = true;")
sb.Append(Page.GetPostBackEventReference(btnSubmit))
sb.Append(";")
btnSubmit.Attributes.Add("onclick", sb.ToString())

 'PREVENT MULTIPLE BUTTON CLICKS ##########################
Public Sub multipleButtonClicks(ByRef myPage As System.Web.UI.Page, ByRef myButton As System.Web.UI.WebControls.Button)

Dim sb As System.Text.StringBuilder = New System.Text.StringBuilder

If myPage.Validators.Count > 0 Then
sb.Append("if (typeof(Page_ClientValidate) == 'function') { ")
sb.Append("if (Page_ClientValidate() == false) { return false; }} ")
End If

sb.Append("this.value = 'Please wait...';")
sb.Append("this.disabled = true;")
sb.Append(myPage.GetPostBackEventReference(myButton))
sb.Append(";")

myButton.Attributes.Add("onclick", sb.ToString())
sb = Nothing
End Sub


YOU CAN CALL IT FROM YOUR PAGE USING:

multipleButtonClicks(Me.Page, Me.Button1)

WHERE Button1 = the name of your button


Hope this helps!
# re: How to prevent multiple clicks of a submit button in ASP.NET 8/9/2004 5:54 AM George Philippakos

ALSO:

With the above code, you don't need to turn off CausesValidation on your buttons, this will allow client side validators to function - BUT remember to add the following line to the PageLoad event:

Page.GetPostBackClientHyperlink(Me, "")

Make sure it runs every time (even when the page posts back on itself)


You are all life savers! Well Done!

Regards,

Aeros

0
 

Author Comment

by:JAMES
Comment Utility
Aeros - was all that in response to my post?!?
0
 

Author Comment

by:JAMES
Comment Utility
Aeros - Thanks for the input but I think I will go with Baan's answer.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
This article discusses the ASP.NET AJAX ModalPopupExtender control. In this article we will show how to use the ModalPopupExtender control, how to display/show/call the ASP.NET AJAX ModalPopupExtender control from javascript, how to show/display/cal…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

728 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now