PostBack twice for a button click?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • pedestrian via DotNetMonster.com

    PostBack twice for a button click?

    I have a simple VB ASP.NET 2.0 page with a Button and a Label.
    The Label text is "0". For each click of the button (btnAdd), it should
    increment the Label text by 1.

    The code:

    Protected Sub btnAdd_Click(.. .) Handles btnAdd.Click
    Trace.Warn("Bef ore add: " & lblCounter.Text )
    lblCounter.Text = (Int32.Parse(lb lCounter.Text) + 1).ToString
    Trace.Warn("Aft er add: " & lblCounter.Text )
    End Sub

    However, each time I click the button, the label always increase
    by 2 instead of the expected 1. What's the problem area?

    I tried to debug using Trace.
    I get the following result from the trace:

    Begin Raise PostBackEvent
    Before add: 0
    After add: 1
    Before add: 1
    After add: 2
    End Raise PostBackEvent

    Is this signifies PostBack event is being run twice per button click?
    How to solve it?

    Thanks for your assistance.

    --
    Regards,
    Pedestrian, Penang.

    Message posted via http://www.dotnetmonster.com

  • Steven Nagy

    #2
    Re: PostBack twice for a button click?

    Hi can you post all code except for designer generated code?
    There's no way that the method should be executing twice.

    Comment

    • pedestrian via DotNetMonster.com

      #3
      Re: PostBack twice for a button click?

      (Default.aspx)
      <%@ Page Language="VB" AutoEventWireup ="true" CodeFile="Defau lt.aspx.vb"
      Inherits="_Defa ult" Trace="true"%>
      <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "
      http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dt d">
      <html xmlns="http://www.w3.org/1999/xhtml" >
      <head runat="server">
      <title>Untitl ed Page</title>
      </head>
      <body>
      <form id="form1" runat="server">
      <div>
      <asp:Button ID="btnAdd" Text="Add" OnClick="btnAdd _Click"
      runat="server" />
      <asp:Label ID="lblCounter " Text="0" runat="server" />
      </div>
      </form>
      </body>
      </html>

      (Default.aspx.v b)
      Partial Class _Default
      Inherits System.Web.UI.P age

      Protected Sub btnAdd_Click(By Val sender As Object, ByVal e As System.
      EventArgs) Handles btnAdd.Click
      Trace.Warn("Bef ore add: " & lblCounter.Text )
      lblCounter.Text = (Int32.Parse(lb lCounter.Text) + 1).ToString
      Trace.Warn("Aft er add: " & lblCounter.Text )
      End Sub
      End Class

      --
      Regards,
      Pedestrian, Penang.

      Message posted via http://www.dotnetmonster.com

      Comment

      • jjoy

        #4
        Re: PostBack twice for a button click?

        Sounds like you might have an event handler in the designer file in
        addition to the specification in the label tag.

        Comment

        • pedestrian via DotNetMonster.com

          #5
          Re: PostBack twice for a button click?

          Thanks. I shall check it up.

          --
          Regards,
          Pedestrian, Penang.

          Message posted via DotNetMonster.c om
          Best online pokies for real money in Australia and learn about safety and game mechanics in this comprehensive guide.


          Comment

          • Damien

            #6
            Re: PostBack twice for a button click?

            pedestrian via DotNetMonster.c om wrote:
            (Default.aspx)
            <%@ Page Language="VB" AutoEventWireup ="true" CodeFile="Defau lt.aspx.vb"
            Inherits="_Defa ult" Trace="true"%>
            <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "
            http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dt d">
            <html xmlns="http://www.w3.org/1999/xhtml" >
            <head runat="server">
            <title>Untitl ed Page</title>
            </head>
            <body>
            <form id="form1" runat="server">
            <div>
            <asp:Button ID="btnAdd" Text="Add" OnClick="btnAdd _Click"
            runat="server" />
            <asp:Label ID="lblCounter " Text="0" runat="server" />
            </div>
            </form>
            </body>
            </html>
            >
            (Default.aspx.v b)
            Partial Class _Default
            Inherits System.Web.UI.P age
            >
            Protected Sub btnAdd_Click(By Val sender As Object, ByVal e As System.
            EventArgs) Handles btnAdd.Click
            Trace.Warn("Bef ore add: " & lblCounter.Text )
            lblCounter.Text = (Int32.Parse(lb lCounter.Text) + 1).ToString
            Trace.Warn("Aft er add: " & lblCounter.Text )
            End Sub
            End Class
            >
            --
            Regards,
            Pedestrian, Penang.
            AutoEventWireup ="true" - Find all of the <control name>_<event name>
            methods add attach them to the <control name>.<event nameevent

            Handles <control name>.<event name- Attach this method to the
            <control name>.<event nameevent.

            So your btnAdd_Click method is being attached to the btnAdd.Click event
            twice, and is hence being called twice. My normal advice would be to
            set AutoEventWireup to false.

            Damien

            Comment

            • pedestrian via DotNetMonster.com

              #7
              Re: PostBack twice for a button click?

              Thanks for valuable answer, Damien.

              Damien wrote:
              >(Default.asp x)
              ><%@ Page Language="VB" AutoEventWireup ="true" CodeFile="Defau lt.aspx.vb"
              >[quoted text clipped - 31 lines]
              >Regards,
              >Pedestrian, Penang.
              >
              >AutoEventWireu p="true" - Find all of the <control name>_<event name>
              >methods add attach them to the <control name>.<event nameevent
              >
              >Handles <control name>.<event name- Attach this method to the
              ><control name>.<event nameevent.
              >
              >So your btnAdd_Click method is being attached to the btnAdd.Click event
              >twice, and is hence being called twice. My normal advice would be to
              >set AutoEventWireup to false.
              >
              >Damien
              --
              Regards,
              Pedestrian, Penang.

              Message posted via DotNetMonster.c om
              Best online pokies for real money in Australia and learn about safety and game mechanics in this comprehensive guide.


              Comment

              Working...