472,791 Members | 1,578 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,791 software developers and data experts.

Button Click event fires only once

Lee
Hi,I'm trying to have a button that fires a click event only once.
What is the best way to do so that after the first button click, any
the following clicks will be ignored.
Nov 18 '05 #1
8 5324
Lee:

After the button has been used you can disable it with:

btnClick.Enabled = False

in its click event.

Fred
"Lee" <na********@hotmail.com> wrote in message
news:6c**************************@posting.google.c om...
Hi,I'm trying to have a button that fires a click event only once.
What is the best way to do so that after the first button click, any
the following clicks will be ignored.

Nov 18 '05 #2
Couldn't you just set the button's enable property to false inside the
on_click method?

Shawn

"Lee" <na********@hotmail.com> wrote in message
news:6c**************************@posting.google.c om...
Hi,I'm trying to have a button that fires a click event only once.
What is the best way to do so that after the first button click, any
the following clicks will be ignored.

Nov 18 '05 #3
Lee
One way would be to use a hidden control on the client that you can set on the client and read from the codebehind to figure out what happened on the client

You can also try this
If you look inside the InitializeComponent() in your code behind you'll find a event handler assignment for your button. Move this line inside your page load method and do it only when it's not a postback(Assuming your postback can only be caused by your button. If there are other controls that can cause a postback then you have to wire the event in all the other postbacks). I should note MS doesn't recommend you modify the contents of IntializeComponent

this.myButton.Click += new System.EventHandler(this.myButton_Click);

HTH
Suresh

----- Lee wrote: ----

Hi,I'm trying to have a button that fires a click event only once
What is the best way to do so that after the first button click, an
the following clicks will be ignored

Nov 18 '05 #4
OMG!! I don't know what I was thinking

Setting Disable property of the button as Shawn and Fred mentioned is the best solution.
Nov 18 '05 #5
Lee,

If you want I created a bit of code you can use. It disables a button after
it's clicked once; but it only disables it after first using .Net's built in
javascripts to make certain that a button doesn't get disabled if a page
isn't valid client side.

If you disable a button on it's first click and the and the page isn't
valid, well, then the user will never be able to submit the page again will
they?

I've placed the script in a javascript component I've built. It's free and
the entire project (v1.1) is available for download from my website,
www.aboutfortunate.com. Just click the "Code Library" link in the top right
and then click the "Javascript" button in the menu on the left. (There are
some other scripts and objects available and everything on the site is free
for the taking and includes all source code.)

--
Sincerely,

S. Justin Gengo, MCP
Web Developer / Programmer

Free code library at:
www.aboutfortunate.com

"Out of chaos comes order."
Nietzche
"Lee" <na********@hotmail.com> wrote in message
news:6c**************************@posting.google.c om...
Hi,I'm trying to have a button that fires a click event only once.
What is the best way to do so that after the first button click, any
the following clicks will be ignored.

Nov 18 '05 #6
"Lee" <na********@hotmail.com> wrote in message
news:6c**************************@posting.google.c om...
Hi,I'm trying to have a button that fires a click event only once.
What is the best way to do so that after the first button click, any
the following clicks will be ignored.


i assume that you don't want to use a server-side solution, which is what
the others suggested.

you can use javascript to disable the button after it's been clicked, so any
subsequent clicks won't get posted to the server. you will have to
experiment with which event to disable the button on, but i'd start with
onclick (could be used by .net for postingback) onmouseup and onmousedown.
then, in whatever event you find works, do

document.all.btnName.enabled = false;

i suspect that not all browsers will support the .enabled property... but
those that don't will be in the minority.

R
Nov 18 '05 #7
Shawn and Fred have shown the server side method. Client side code can do
this, as Rimu pointed out.

However, if the button is supposed to use any Serverside validations in its
processing, then if the validations fail, the client would have already
disabled the button.

Just something to consider.

"Rimu Atkinson" <ri***@paradise.net.removenospamthing.nz> wrote in message
news:lM********************@news02.tsnz.net...
"Lee" <na********@hotmail.com> wrote in message
news:6c**************************@posting.google.c om...
Hi,I'm trying to have a button that fires a click event only once.
What is the best way to do so that after the first button click, any
the following clicks will be ignored.
i assume that you don't want to use a server-side solution, which is what
the others suggested.

you can use javascript to disable the button after it's been clicked, so

any subsequent clicks won't get posted to the server. you will have to
experiment with which event to disable the button on, but i'd start with
onclick (could be used by .net for postingback) onmouseup and onmousedown.
then, in whatever event you find works, do

document.all.btnName.enabled = false;

i suspect that not all browsers will support the .enabled property... but
those that don't will be in the minority.

R

Nov 18 '05 #8
Lee
Hi, Thanks for all the replies.

btnClick.Enabled = False won't work for me because the button get
disabled only after the page is postback.

I have came up my own simple solution and it worked fine.

First I put Session["ButtonClicked"] = false inside of Page_Load and
then in the button click event I put

if( Session["ButtonClicked"] != null ){
Session.Remove("ButtonClicked");
//.... other codes
}

So after the first button click, it will ignore the codes inside of
the button event.
Lee
Nov 18 '05 #9

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

Similar topics

1
by: Big_Stu | last post by:
Hi all, I have an <asp:button> inside an <asp:table> and I am finding that the button click event doesn't fire at all! I have added the button to the initialize component event, and the table. ...
6
by: Michael Johnson Jr. | last post by:
I am trying to handle a button click event, which updates a web control table with data. The button is dynamically created in the table itself. When I call updateTable() in the Page_Load the new...
4
by: Mark Lingen | last post by:
I've found a problem with postback event handling and webcontrol buttons. Try out the following code in an ASP.Net project and you will see. Create a web project in VB.Net and drop this code...
3
by: Rea Peleg | last post by:
Hi all This is so Bizarre - asp button click event is working perfectly on one page. I duplicated this page in order to use it again with minor modifications but there the button wont work.. ...
1
by: Roger | last post by:
Have a grid and when I edit a grid row it goes into edit mode. Once I attempt to click a button on the form; once the grid losses focus, it fires my beforerowupdate event. Runs through my code, but...
17
by: Eric | last post by:
I'm new to JavaScript and I wrote this code to play with. Oddly, if I enter text in a box and then press the button, I only get the onChange event for the text box and not the button's onclick...
8
by: Learner | last post by:
Hello, I converted a VS 2003 project to VS 2005. It works perfectly alright in VS 2003 but when I try running in VS 2005 I got strange thing going on. The code (my button click event ) runs...
5
by: saadkhan | last post by:
i want to a buttin click event when a <div> is clicked. That is when div tag`s onclick event fires, it directs to button click event..... <div id="Div" onclick="????????"> HEADER </div>...
4
yarbrough40
by: yarbrough40 | last post by:
can any one tell me if there is a way to pass a string variable to a button click event? ultimately I am looking to fire client side Javascript "__doPostBack('Button1','click','')" and have the...
0
by: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
0
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Sept 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
0
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: lllomh | last post by:
How does React native implement an English player?
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

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.