开发者

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

like I want the user to send some info only once ..actually have se开发者_如何转开发nt 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


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.


Beware, pseudo-code ahead:

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


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).


put a hidden field and assign the value to it when the user first click the button.So check the value of hidden field and compare.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜