Button Click event fires only once

L

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.
 
S

Shawn

Couldn't you just set the button's enable property to false inside the
on_click method?

Shawn
 
F

Fred Nelson

Lee:

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

btnClick.Enabled = False

in its click event.

Fred
 
G

Guest

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
 
G

Guest

OMG!! I don't know what I was thinking

Setting Disable property of the button as Shawn and Fred mentioned is the best solution.
 
S

S. Justin Gengo

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
 
R

Rimu Atkinson

Lee said:
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
 
B

Bryan Donaldson

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.
 
L

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
 

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

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top