473,591 Members | 2,842 Online
Bytes | Software Development & Data Engineering Community
+ 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(La bel1.Text) + 1).ToString
End Sub
...(omitted)
<asp:Button ID="Button1" Text="Add" OnClick="Button 1_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 1515
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.c om" <u16758@uwewrot e in message
news:67591aca19 3c8@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(La bel1.Text) + 1).ToString
End Sub
..(omitted)
<asp:Button ID="Button1" Text="Add" OnClick="Button 1_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
1545
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 clickable because the arrow your mouse displays as you browse from page to page, does not transform to the pointer--something that natuarally happens when you hover over a hyperlink. How may I provide this functionality? Thanks,
2
4430
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 on the image (it is a link to the content page) it changes to a third image AND STAYS THERE. ie: so the iamge won't change back to the omouseoff image. Does this make sense? Also, how do I "preload" the images before they are even needed? eg:...
0
1971
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 want to get the BigClassList.SelectedItem.Text and BigClassList.SelectedItem.Value in a button onclick method,btnSmallClassEdit_Click(....),there post an error " System.NullReferenceException: 佫¶ÔÏóÒýÓÃÉèÖõ½¶ÔÏóµÄʵÀý¡£", I Don't know the...
4
1396
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 the string. I also have asp server controls(labels) in the datalist. When the form is processed, I am retrieving the values of the radio buttons through Request.form. To retrieve the values of the asp server controls , I am using FindControl...
2
2082
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 CheckBoxList myCheckBoxList;
3
1906
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 login, user is directed to a page called view.aspx which shows the user the files in a directory and allows them to delete the files. This page has a data grid having 4 columns - delete button, File Name, Last Write Time and File Size. When a user...
0
1480
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 engine, but with a difference: when you click a link, instead of go to a page, a text will be added above the link, like expanding the page on the line above the link. The idea: The idea is populate a gridview where each row is a link. When the user
13
2372
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 content page that has controls that I am trying to get onclick to work for. // ===================== // MyScriptFile.js // =====================
2
3304
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 user clicked nothing. The href is what i use for my tooltip so it's only surppose to work onmouseover! This is the jscript: onClick="document.getElementById('__Submit').focus();"
0
7934
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8236
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8362
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
7992
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8225
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5400
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
3850
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
1465
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1199
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.