Keeping track of number of button clicks

僤鯓⒐⒋嵵緔 提交于 2019-12-06 10:38:34

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; }
}

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.

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