By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,574 Members | 2,960 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,574 IT Pros & Developers. It's quick & easy.

Button Click event fires only once

P: n/a
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
Share this Question
Share on Google+
8 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
"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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.