473,473 Members | 1,999 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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

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?

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

Oct 6 '06 #1
2 1512
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]

"pedestrian via DotNetMonster.com" <u16758@uwewrote in message
news:67591aca193c8@uwe...
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?

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

Oct 6 '06 #2
Thanks Ken.

--
Regards,
Pedestrian, Penang.

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

Oct 6 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Spank | last post by:
Hello, I have a thumbnail image with the onclick event. When clicked, a larger pic is displayed via a javascript funtion. When browsing the page, the client does not realize the image is...
2
by: Rob Manger | last post by:
Hi All, I am hoping someone can help me. I am trying to setup my main page so that when the user moves the mouse over an image, it changes the source (got this working). When the user CLICKS...
0
by: huobazi | last post by:
I have many dropdownlist controls in my ascx (and use LoadControl in a aspx fiel) file,so i write a method "InitList(DropDownList list,string strsql,string TextField,string ValueField)" but when i...
4
by: ani | last post by:
I have a datalist control in my aspx page(this is paginated and everytime submits to itself). I am calling a function within my datalist that returns a html control (radio button ) in the form of...
2
by: Nemo | last post by:
Hi. I Have a CheckBoxList and I would like the add the onclick attribute. But when I add the attribute only the first one in my list get the onclick attribute. Here is my code. protected...
3
by: Joe | last post by:
Hi, I have written a webpage that allows a user to delete files in asp.net with I am having a small problem. To access this page a user has to login via login.aspx page. After successful...
0
by: Beto | last post by:
I am working on a project and I was thinking about implement ajax instead of using pure asp.net, however I've got a problem. The page: the page is like a google, yahoo or any other search...
13
by: pagates | last post by:
Hello All-- I'm having a problem getting an onclick event to work. Here is my setup: I have an external JavaScript file, a master page that registers the javascript as an external file, and a...
2
by: ismailc | last post by:
Hi, I would like to do the following. I have an href where onclick the focus is on the href: I got it to change focus to another object but i don't really want that. It must be as if the...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.