PostBack twice for a button click?

  • Thread starter pedestrian via DotNetMonster.com
  • Start date
P

pedestrian via DotNetMonster.com

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("Before add: " & lblCounter.Text)
lblCounter.Text = (Int32.Parse(lblCounter.Text) + 1).ToString
Trace.Warn("After 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.
 
S

Steven Nagy

Hi can you post all code except for designer generated code?
There's no way that the method should be executing twice.
 
Joined
Oct 1, 2006
Messages
1
Reaction score
0
Check the InitializeComponent() function. The Visual Studio forms designer sometimes adds the handler to the event twice, so you might see:

this.btnSave.Click += new System.EventHandler(this.btnSave_Click);
this.btnSave.Click += new System.EventHandler(this.btnSave_Click);

or the equivalent for your button.
 
P

pedestrian via DotNetMonster.com

(Default.aspx)
<%@ Page Language="VB" AutoEventWireup="true" CodeFile="Default.aspx.vb"
Inherits="_Default" Trace="true"%>
<!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:Button ID="btnAdd" Text="Add" OnClick="btnAdd_Click"
runat="server" />
<asp:Label ID="lblCounter" Text="0" runat="server" />
</div>
</form>
</body>
</html>

(Default.aspx.vb)
Partial Class _Default
Inherits System.Web.UI.Page

Protected Sub btnAdd_Click(ByVal sender As Object, ByVal e As System.
EventArgs) Handles btnAdd.Click
Trace.Warn("Before add: " & lblCounter.Text)
lblCounter.Text = (Int32.Parse(lblCounter.Text) + 1).ToString
Trace.Warn("After add: " & lblCounter.Text)
End Sub
End Class
 
J

jjoy

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

Damien

pedestrian said:
(Default.aspx)
<%@ Page Language="VB" AutoEventWireup="true" CodeFile="Default.aspx.vb"
Inherits="_Default" Trace="true"%>
<!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:Button ID="btnAdd" Text="Add" OnClick="btnAdd_Click"
runat="server" />
<asp:Label ID="lblCounter" Text="0" runat="server" />
</div>
</form>
</body>
</html>

(Default.aspx.vb)
Partial Class _Default
Inherits System.Web.UI.Page

Protected Sub btnAdd_Click(ByVal sender As Object, ByVal e As System.
EventArgs) Handles btnAdd.Click
Trace.Warn("Before add: " & lblCounter.Text)
lblCounter.Text = (Int32.Parse(lblCounter.Text) + 1).ToString
Trace.Warn("After add: " & lblCounter.Text)
End Sub
End Class

AutoEventWireup="true" - Find all of the <control name>_<event name>
methods add attach them to the <control name>.<event name> event

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

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
 
P

pedestrian via DotNetMonster.com

Thanks for valuable answer, Damien.
(Default.aspx)
<%@ Page Language="VB" AutoEventWireup="true" CodeFile="Default.aspx.vb"
[quoted text clipped - 31 lines]
Regards,
Pedestrian, Penang.

AutoEventWireup="true" - Find all of the <control name>_<event name>
methods add attach them to the <control name>.<event name> event

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

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.com
http://www.dotnetmonster.com/Uwe/Forums.aspx/asp-net/200610/1
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,144
Latest member
KetoBaseReviews
Top