Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Webcontrol button click event being fired when browser Refresh button pressed

Posted on 2004-09-21
4
Medium Priority
?
1,555 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
[X]
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
  • 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

Enroll in September's Course of the Month

This month’s featured course covers 16 hours of training in installation, management, and deployment of VMware vSphere virtualization environments. It's free for Premium Members, Team Accounts, and Qualified Experts!

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…
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 …
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

722 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