Click here to Skip to main content
15,888,610 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello,
I have a problem I want to solve through javascript if possible.
in my project has a button that is responsible for invoking a function (in javascrip) and perform some actions. I want the user to perform this action only 3 times. ie I want only 3 clicks on the button, and after the third click to the button is disabled or it disappears from the screen. this to prevent the user makes more than 3 actions which are permitted. I tried to sort through the cycle (if or whatever) inside the button click in JavaScript function but could not because each click generates a postback and then lose the value that I try to use to track clicks. I do not know if it is possible to resolve this through viewstate, where I keep the number of clicks and get 3 if I do a cycle that disables the button. can someone help me? or suggest other ways to solve this problem?
Posted
Updated 25-Nov-13 11:26am
v2
Comments
Sergey Alexandrovich Kryukov 25-Nov-13 17:40pm    
I wonder: why? Don't you think it could be highly confusing for a user.
—SA

View state? Are you saying that there should be postbacks anywhere between these three clicks? If so, storing the number of clicks in Javascript (normally quite possible) would be useless, as after the postback the Javascript state machine for the page is fully reset, created from scratch. In this case, the use of viewstate variable would be quite an adequate way of working around, which is quite easy: just store then number is some view state variable and update in the server-side click handler Please see:
http://msdn.microsoft.com/en-us/library/ms972976.aspx[^],
http://msdn.microsoft.com/en-us/library/ms228048%28v=vs.85%29.aspx[^],
http://msdn.microsoft.com/en-us/library/ms227551%28v=vs.85%29.aspx[^].

However, if there are no postbacks during all the click, you can use Javascript and client side along, as it supports the closure functionality:
http://en.wikipedia.org/wiki/Closure_%28computer_science%29[^].

Say, you can work with some event handler like this:
JavaScript
clickLimit = 3; // some value
var clickCount = 0; // there are no "static fields" in Javascript, so it can be a local variable just visible below

function ClickHandler() {
    if (clickCount > clickLimit) {
        //...
    }
    ++clickCount; // do you to effect of the closure, remains accessible at least as long as this function is used for handling some events
    //...
}


—SA
 
Share this answer
 
Hi
Try this code..



XML
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication1.WebForm1" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script src="jquery.js.js"></script>
    <script type="text/javascript">

        var icount = 0;
        var javascriptFunction = function (buttonControl)
        {

            // ur custom coding here...

            icount++;
            if (icount > 2)
                buttonControl.disabled = true;


            return false;
        }


    </script>


</head>
<body>
    <form id="form1" runat="server">


        <asp:Button ID="btnTest" Text="Test Button" runat="server" OnClientClick="return javascriptFunction(this)" />


    </form>
</body>
</html>
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900