Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Webcontrol button click event being fired when browser Refresh button pressed

Posted on 2004-09-21
4
Medium Priority
?
1,556 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 2000 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

I have developed many web applications with asp & asp.net and to add and use a dropdownlist was always a very simple task, but with the new asp.net, setting the value is a bit tricky and its not similar to the old traditional method. So in this a…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
Integration Management Part 2
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…

916 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