Solved

Webcontrol button click event being fired when browser Refresh button pressed

Posted on 2004-09-21
4
1,554 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 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

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

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

I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
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 …

627 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