ASP.NET 2.0 : Is there a need for OnClick attribute in .aspx file?

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

pedestrian via DotNetMonster.com

I'm having an issue with the button click event handler.

I follow this book example using the single-file method.
It is a very simple page with a Button and a label:

<script runat="server">
Sub Button1_Click(...)
Label1.Text = (Int32.Parse(Label1.Text) + 1).ToString
End Sub
...(omitted)
<asp:Button ID="Button1" Text="Add" OnClick="Button1_Click" runat="server" />
<asp:Label ID="Label1" Text="0" runat="server" />
...

This works as expected where a click on Button1 increase the
Label text by 1.

However, when I move the button handler part to the code behind file, a
Button1 click increase the Label text by 2! which is something I consider
strange.
But when I remove the OnClick attribute in the .aspx, it works as expected
again.
Is this signifies that if I'm using the code-behind method for button click
event
handler, there's ALWAYS no need to specify the OnClick attribute for the asp:
Button element?
 
K

Ken Cox [Microsoft MVP]

Does your Button1_Click(...) have a Handles clause?

If so, you're probably seeing the event fire twice - once for the Handles
and once for the declarative OnClick statement.

AutoEventWireup can also cause a similar problem in C#.

Ken
Microsoft MVP [ASP.NET]
 

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

Top