Home > front end >  ASP.NET Button Click Events Repeat
ASP.NET Button Click Events Repeat

Time:07-20

I have a requirement that the current browser window refreshes at regular intervals to keep the current session active. Below is the JavaScript code that is refreshing the page every 5 minutes.

<script language="javascript">
    setTimeout(function () {
        window.location.reload(1);
    }, 300000);
</script>

It accomplishes the task, but I am noticing that button click events repeat the function at every page refresh. A simple example is provided below. On button click, execute MyFunction();

        protected void TestButton_Click(object sender, EventArgs e)
    {
        MyFunction();
    }

With a 5 minute page refresh interval, the MyFunction() is executing every five minutes.

enter image description here

It seems to only happen for button click events. I changed the interval from 10 seconds to 5 minutes, and the same result is observed; however, it does not seem to happen 100% of the time.

Is there a better way to auto-refresh an ASP.NET page to keep the session active that can avoid this issue, or a way to mitigate the problem being described? It seems like the event is cached and re-executed upon each page refresh, but I'm not sure why.

CodePudding user response:

Your answer might be the same as posted here

The reason for this is your refreshing the last information sent to the server. Which is the button click information in the __doPostback. This is why you are seeing the event of the button fire again.

  • Related