Keeping track of number of button clicks

試著忘記壹切 提交于 2019-12-08 02:03:58

问题


Let us assume that I have a page with a CAPTCHA image.

I want to let the user try to enter the code for three times, otherwise he is not allowed to do it anymore.

How can I keep track of the number of times the "Confirm" button has been clicked. The "Confirm" button has to perform a postback to the server every time it is clicked.

Using JavaScript is not good since if the user reloads the page, the counter would be set to zero. How can this be done please?


回答1:


This should be pretty straight forward. First set the counter to zero, and then update it on subsequent post backs:

if (!this.IsPostBack) { Session["RetryCount"] = 1; }
else
{
    int retryCount = (int)Session["RetryCount"];
    if (retryCount == 3) { // do something because it's bad }
    else { retryCount++; Session["RetryCount"] = retryCount; }
}



回答2:


Use ViewState to store the number.

Or better yet - Session.

The advantage of Session is that it is stored on the Server (AFAIK) so it can't be tampered with, and also that it will persist even when reloading the page.



来源:https://stackoverflow.com/questions/17412239/keeping-track-of-number-of-button-clicks

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!