简体   繁体   中英

How to know how many times a button has been clicked?

like I want the user to send some info only once ..actually have sent a link thru email that opens a page with text box in it ..In that user sends some info by clicking a button..now I dont want user to use that link over and over to send the info ... ..so how do i go abt it ? hope the ques isnt confusing

Beware, pseudo-code ahead:

if (MyButton.Enabled) {
    MyButton.Enabled = false;
    SendInfo();
}

To me it seems that he wants each user to send information using that form once.

If I'm correct in my interpretation, you could send through a unique id with each email and send it through with the form data (along with the user's email).

On the server, you'd have a database table which maps each email with a unique id. Whenever a user fills out the form, you'd check the id against the email and set a column in the table indicating that the user has already sent data with the form.

The first thing you'd do when receiving data is to check the database and check if the data has already been sent. If not, accept the data otherwise you could display an error message or something.

Depending on how rigorous you wish to be you could add a cookie to the users response the first time they click it which indicates they have clicked already and then ignore any further clicks if that cookie exists on subsequent requests. Also you could then disable the control when the page is viewed if they have this cookie. Otherwise if they are a logged in user you could store the record of the click in a DB and reject subsequent clicks by checking the DB for a record of this user clicking this control.

Here is some code to demo:

 public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            var ctrl = sender as WebControl;
            if (!HasButtonBeenClicked(ctrl.ClientID))
            {
                Response.Write("Click accepted");
            }
            else
            {
                Response.Write("You have clicked this already!");
            }
        }

        private bool HasButtonBeenClicked(string controlId)
        {
            if (Request.Cookies["has_clicked_"+ controlId] != null)
            {
                return true;
            }
            var cookie = new HttpCookie("has_clicked_" + controlId);
            Response.Cookies.Add(cookie);
            return false;
        }


    }
bool isButtonClicked = false; // this will be held in global

in button click event set isButtonClick = true;

button1_Click()
{
    if(!isButtonClicked)
    {
    // which means button is clicked once, put your action code here
    isButtonClicked = true;
    }
}

A lot of ways to do that.... For example:

int _counter = 0;

void btnCounter_click()     
{
  _counter++;  
}

EDITED:

just disable the button, after the data has been sent..

From your description - it appears something like survey page where you want to ensure only one submittal per user. This can be achieved by creating a unique identifier per user and then including that value within your link/form. On server side, you have to ignore repeat submits with same identifiers (or pick the last one).

当用户第一次单击按钮时,放置一个隐藏字段并为其分配值,因此请检查隐藏字段的值并进行比较。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM