Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Webcontrol button click event being fired when browser Refresh button pressed

Posted on 2004-09-21
4
1,548 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
ID: 12111471
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
ID: 12111539
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
ID: 12111571
Aeros - was all that in response to my post?!?
0
 

Author Comment

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

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

790 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