Button Click Fires Twice

Last post 11-06-2008 6:10 PM by worldspawn[]. 6 replies.

Sort Posts:

  • Button Click Fires Twice

    11-04-2008, 6:13 PM
    • Contributor
      2,348 point Contributor
    • geosync
    • Member since 03-26-2006, 8:44 PM
    • Boston, MA, USA
    • Posts 740

    I've seen events fire twice if called from Page_Load; these issues are PostBack related.

    But I don't understand my current situation:

    My admin page called User Mgmt page has a Change Password button. 

    Click Change Password, and password is changed.  Also, email is sent.

     However, the code runs twice, and two emails are sent.

    Why?

    ~ Timing is Everything! ~
  • Re: Button Click Fires Twice

    11-04-2008, 7:09 PM
    • Contributor
      4,447 point Contributor
    • worldspawn[]
    • Member since 01-19-2003, 1:21 PM
    • Melbourne, Australia
    • Posts 934

     Could be the position of mars in relation to uranus... no wait thats not right, post your code!!

    -- Sam Critchley

    "Wise man say 'forgiveness is divine, but never pay full price for late pizza." - TMNT

    website design
    internet marketing
  • Re: Button Click Fires Twice

    11-04-2008, 9:22 PM
    • Contributor
      2,348 point Contributor
    • geosync
    • Member since 03-26-2006, 8:44 PM
    • Boston, MA, USA
    • Posts 740

    Ah, code...I have reduced these files almost as far as they can go.  Let me know what you think:

     

    <%@ Page Language="VB" AutoEventWireup="false" CodeFile="UserMgmt.aspx.vb" Inherits="UserMgmt" %>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head runat="server">
        <title>Untitled Page</title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
        <asp:Label ID="lblMain" runat="server"></asp:Label><br />
        <asp:Label ID="lblSub" runat="server" Text="Change Password"></asp:Label><br />
        <asp:Label ID="Label1" runat="server" AssociatedControlID="tbOldPwd" Text="Old:"></asp:Label>
        <asp:TextBox ID="tbOldPwd" runat="server"></asp:TextBox><br />
        <asp:Label ID="Label24" runat="server" AssociatedControlID="tbNewPwd" Text="New:"></asp:Label>
        <asp:TextBox ID="tbNewPwd" runat="server"></asp:TextBox><br />
        <asp:Label ID="Label25" runat="server" AssociatedControlID="tbConfirm" Text="Confirm:"></asp:Label>
        <asp:TextBox ID="tbConfirm" runat="server"></asp:TextBox><br />
        <asp:Button ID="btnChgPwd" runat="server" OnClick="btnChgPwd_Click" 
          OnClientClick="return confirm('Change password?')" Text="Change Password" /><br />
        <asp:CheckBox ID="cbSendEmail" runat="server" Checked="True" Text="Email User" />   
        </div>
        </form>
    </body>
    </html>
    

     

    1    Partial Public Class UserMgmt
    2      Inherits System.Web.UI.Page
    3    
    4      Private usr As MembershipUser
    5    
    6      Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
    7    
    8        If Not Page.IsPostBack Then
    9          ' Pickup UserName from Session.
    10         If Session("UserName") IsNot Nothing Then
    11           ' Get membership object
    12           usr = Membership.GetUser(Session("UserName").ToString)
    13           ' Store membership object.
    14           Session("USER") = usr
    15           lblMain.Text = "Managing Details for " + usr.UserName
    16         End If
    17       Else
    18         ' On postbacks, retrieve membership object from session.
    19         usr = CType(Session("USER"), MembershipUser)
    20       End If
    21   
    22     End Sub
    23   
    24     Protected Sub btnChgPwd_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnChgPwd.Click
    25   
    26       Try
    27         If tbNewPwd.Text <> "" Then usr.ChangePassword(tbOldPwd.Text, tbNewPwd.Text)
    28   
    29         If cbSendEmail.Checked Then
    30           Dim mm As New MailMessage
    31           mm = New MailMessage
    32           mm.From = New MailAddress("name@domain.com")
    33           mm.To.Add(New MailAddress(usr.Email))
    34           mm.Subject = "Subject Goes Here"
    35           mm.Body = "Message text goes here."
    36   
    37           Dim sc As New SmtpClient()
    38           sc.Send(mm)
    39         End If
    40       Catch eX As Exception
    41   
    42       Finally
    43   
    44       End Try
    45   
    46     End Sub
    47   End Class
    48   
    49   
    
     
    ~ Timing is Everything! ~
  • Re: Button Click Fires Twice

    11-04-2008, 10:57 PM
    • Contributor
      4,447 point Contributor
    • worldspawn[]
    • Member since 01-19-2003, 1:21 PM
    • Melbourne, Australia
    • Posts 934

     Ok... no suggestions sorry Tongue Tied Does it still happen if your remove the confirm script?

    -- Sam Critchley

    "Wise man say 'forgiveness is divine, but never pay full price for late pizza." - TMNT

    website design
    internet marketing
  • Re: Button Click Fires Twice

    11-05-2008, 1:16 AM
    • Contributor
      2,348 point Contributor
    • geosync
    • Member since 03-26-2006, 8:44 PM
    • Boston, MA, USA
    • Posts 740

    For now, my workaround uses ViewState to restrict event processing to one pass:

    If tbNewPwd.Text <> "" Then usr.ChangePassword(tbOldPwd.Text, tbNewPwd.Text)
    
    If cbSendEmail.Checked And ViewState("MailSent") Is Nothing Then
      Dim mm As New MailMessage
      mm = New MailMessage
      mm.From = New MailAddress("name@domain.com")
      mm.To.Add(New MailAddress(usr.Email))
      mm.Subject = "Subject Goes Here"
      mm.Body = "Message text goes here."
    
      Dim sc As New SmtpClient()
      sc.Send(mm)
    
      ViewState("MailSent") = "TRUE"
    End If
    
    ~ Timing is Everything! ~
  • Re: Button Click Fires Twice

    11-05-2008, 2:25 AM
    Answer
    • Contributor
      4,447 point Contributor
    • worldspawn[]
    • Member since 01-19-2003, 1:21 PM
    • Melbourne, Australia
    • Posts 934

    Ok did a quick google.

    The solution (maybe) is to remove the "Handles btnChgPwd.Click" bit from your handler definition. I think having that plus the OnClick bit in your aspx code essentially binds the method twice to the event, thus executes it twice. That's VB for ya! Stick out tongue

    My info came from:
    http://bytes.com/forum/thread643743.html 
    -- Sam Critchley

    "Wise man say 'forgiveness is divine, but never pay full price for late pizza." - TMNT

    website design
    internet marketing
  • Re: Button Click Fires Twice

    11-06-2008, 6:10 PM
    • Contributor
      4,447 point Contributor
    • worldspawn[]
    • Member since 01-19-2003, 1:21 PM
    • Melbourne, Australia
    • Posts 934

     Hey was I right?

    -- Sam Critchley

    "Wise man say 'forgiveness is divine, but never pay full price for late pizza." - TMNT

    website design
    internet marketing
Page 1 of 1 (7 items)