4

What i trying to do is simply disable a button after 4 clicking but when the post backs occurs then it doesn't have a count of clicks. i trying to do that by jquery. Any suggestions.?
Correct me if i am wrong by asp.net we do that by putting a static variable and increment the count on button click .

joshua
  • 2,371
  • 2
  • 29
  • 58
  • 2
    I would use an hidden field and store the number of clicks in there, this is persisted across post backs and you cah get its value from JQuery client side. – Davide Piras Dec 09 '11 at 11:44
  • Do you only need to disable the button after 4 clicks, or does the server have to react on these clicks somehow as well? because in the former case, I would completely stay on the client side and not post back anything at all... – codeling Dec 09 '11 at 11:45
  • i only need to disable it like preventing for login Again for today.I already handled that in code behind.My logic behind that is check login parameter pass it on webservice if attempt is more that 4 than block the user for login today. – joshua Dec 09 '11 at 11:49
  • Why not try the cookie approach ? – Vamsi Dec 09 '11 at 12:01

5 Answers5

3

Just keep the count in ViewState

For example

public int ClickCount
{
    get
    {
        return (int)(ViewState["ClickCount"] ?? 0);
    }
    set
    {
        ViewState["ClickCount"] = value;
    }
}
Richard Friend
  • 15,800
  • 1
  • 42
  • 60
0

you can do that by saving value into hidden field and retrieving it via jquery. but that won't be a good solution as anyone who has html knowledge might change the value of hidden field to enable the button.

Junaid
  • 1,708
  • 16
  • 25
  • In Backend i storing the count More like say login button click .If users attempted 4 times wrong than i blcoking for login.so i need to pass click counts.Now I thinking viewstate is better to use rather than thinking about jquery – joshua Dec 09 '11 at 11:53
0

something like this?

window.clicks = 0;
$("#mybotton").click(function(){if(window.clicks++ >= 3)$(this).disable();});
El'
  • 401
  • 7
  • 19
0

Put this in ready of jquery

///count number of button click
    var counter = 0;
    function CountClicks() {
        counter++;
        if (counter > 4) {
            alert("disable button here.");
            return false;
        }}

Call CountClicks on button client click

Neha
  • 2,933
  • 3
  • 19
  • 23
  • I think HTTP stateless protocol and take every request as new one.never cares about what the previous is.. – joshua Dec 09 '11 at 12:30
  • http://stackoverflow.com/questions/4701349/jquery-increase-the-value-of-a-counter-when-a-button-is-clicked check this out – Neha Dec 09 '11 at 12:34
  • That is correct as you staying on the same page. but not for navigating between page on click. – joshua Dec 09 '11 at 12:37
  • then go as david suggest use a hidden field to get your counter – Neha Dec 09 '11 at 12:39
0

in CodeBehind create a simplest

private static int counter = 0;

validating IsPostBack property you can

    if (Page.IsPostBack)
        counter++;

where you want

    Button1.Enabled = (counter<4);

i suggest to increment the counter in Button1_Click event

manuellt
  • 669
  • 2
  • 7
  • 19