简体   繁体   English

如何知道一个按钮被点击了多少次?

[英]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 ? 就像我希望用户仅发送一次信息..实际上已经通过电子邮件发送了一个链接,该链接打开了一个带有文本框的页面..在那个用户中,通过单击按钮发送了一些信息..现在我不希望用户使用该链接一遍又一遍地发送信息..so,我该怎么办呢? 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). 如果我的解释正确,那么您可以通过每封电子邮件发送唯一的ID,并随表格数据一起发送(连同用户的电子邮件)。

On the server, you'd have a database table which maps each email with a unique id. 在服务器上,您将具有一个数据库表,该表映射具有唯一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. 每当用户填写表单时,您都将对照电子邮件检查ID,并在表中设置一列,表明该用户已经使用该表单发送了数据。

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. 根据您希望的严格程度,您可以在用户第一次单击Cookie时向用户响应添加cookie,这表示他们已经单击过,然后在后续请求中存在该cookie时忽略任何进一步的点击。 Also you could then disable the control when the page is viewed if they have this cookie. 如果他们有这个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; 在按钮单击事件中设置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). 在服务器端,您必须忽略具有相同标识符的重复提交(或选择最后一个)。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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