Image button Multiple Click

Last post 10-17-2008 2:01 AM by generalproblem. 5 replies.

Sort Posts:

  • Image button Multiple Click

    09-29-2008, 4:40 AM
    Hello All, I have an image button on my form which acts as a submit button. I needed to control the behavior so that it can be clicked only once to avoid problems. how can I do that? Regards, GeneralProblem
    Filed under:
  • Re: Image button Multiple Click

    09-29-2008, 7:28 AM
    • Member
      408 point Member
    • member1
    • Member since 08-20-2008, 3:18 PM
    • Posts 59

    Hi,

    use the following approach

    1. for the OnClientClick property of the ImageButton add a  javascript function like

        <asp:ImageButton ID="ImageButton1" runat="server" OnClientClick = "Javascript:DisableDoubleClick()"/>

    2. Add the javascript as below

       <script type = "text/javascript">

                  var count = 0;

                  function DisableDoubleClick()

                  {

                       count ++;

                       if (count > 1 )

                        {

                             event.returnValue=false;

                              event.cancel = true;

                         }

                   }

     </script>

     

    Hope this helps....

    Bharath
  • Re: Image button Multiple Click

    09-29-2008, 7:40 PM
    Answer
    • Star
      13,971 point Star
    • yasserzaid
    • Member since 09-22-2007, 9:10 PM
    • Egypt
    • Posts 2,597

    Hi

    check this link to prevent users from submit twice

    http://www.4guysfromrolla.com/webtech/100406-1.shtml

    Good Luck

  • Re: Image button Multiple Click

    09-30-2008, 5:53 AM
    Thanks Bharath for answer. I have tested it and it is not working. But I have a doubt like when the page first gets loaded var count will be initialized to zero. once user submits it and again at a later point of time if he required to submit again what will be the count?
  • Re: Image button Multiple Click

    10-03-2008, 5:46 AM
    Answer

    generalproblem:
    Thanks Bharath for answer. I have tested it and it is not working. But I have a doubt like when the page first gets loaded var count will be initialized to zero. once user submits it and again at a later point of time if he required to submit again what will be the count?
     

     Hi,

    After postback, the page will be processde and then regenerated, so the  value of the  "counter" will be zero again.

    The article provided by Yasser is fairly useful when you want to prevent the users from submitting a form twice when the page being processed.

    If you want to allow users to click the button only once, that means the button is not accessible even after the first processing, you can use a session variable as a counter like this:

    Global.asax:

     

     void Session_Start(object sender, EventArgs e) 
    {
    // Code that runs when a new session is started
    Session["counter"] = 0;

    }

    submit page:

     

     protected void Button1_Click(object sender, ImageClickEventArgs e)
    {
    if (Session["counter"] != null && (int)(Session["counter"]) == 0)
    {
    // logic code
    Session["counter"] = (int)Session["counter"] + 1;
    ImageButton1.Enabled = false;
    }

    }
    Have a nice day! 
    Forward Sun
    Microsoft Online Community Support
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
  • Re: Image button Multiple Click

    10-17-2008, 2:01 AM

     Hello,

     I am facing a problem in this approach like this, I am using both asp validators and javascript some places. so whenever i click on submit and error occurs the user is unable to do any processing thereafter as the counter remains same. How can I reset the counter when error occurs.

     Regards,

    GeneralProblem

Page 1 of 1 (6 items)