Button Click event fires only once

Discussion in 'ASP .Net' started by Lee, Feb 10, 2004.

  1. Lee

    Lee Guest

    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.
     
    Lee, Feb 10, 2004
    #1
    1. Advertising

  2. Lee

    Shawn Guest

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

    Shawn

    "Lee" <> wrote in message
    news:...
    > 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.
     
    Shawn, Feb 10, 2004
    #2
    1. Advertising

  3. Lee

    Fred Nelson Guest

    Lee:

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

    btnClick.Enabled = False

    in its click event.

    Fred


    "Lee" <> wrote in message
    news:...
    > 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.
     
    Fred Nelson, Feb 10, 2004
    #3
  4. 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
     
    =?Utf-8?B?U3VyZXNo?=, Feb 10, 2004
    #4
  5. IGNORE MY SOLUTION>>RE: Button Click event fires only once

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

    Setting Disable property of the button as Shawn and Fred mentioned is the best solution.
     
    =?Utf-8?B?U3VyZXNo?=, Feb 10, 2004
    #5
  6. 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" <> wrote in message
    news:...
    > 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. Justin Gengo, Feb 10, 2004
    #6
  7. "Lee" <> wrote in message
    news:...
    > 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
     
    Rimu Atkinson, Feb 10, 2004
    #7
  8. 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" <> wrote in message
    news:lMcWb.23305$...
    > "Lee" <> wrote in message
    > news:...
    > > 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
    >
    >
     
    Bryan Donaldson, Feb 11, 2004
    #8
  9. Lee

    Lee Guest

    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
     
    Lee, Feb 12, 2004
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Purvi T
    Replies:
    0
    Views:
    934
    Purvi T
    Oct 19, 2004
  2. Techsatish
    Replies:
    0
    Views:
    485
    Techsatish
    Aug 25, 2006
  3. Max2006
    Replies:
    2
    Views:
    1,640
    Walter Wang [MSFT]
    Sep 19, 2007
  4. Replies:
    0
    Views:
    250
  5. Replies:
    3
    Views:
    1,035
Loading...

Share This Page