简体   繁体   中英

Calling server side event handler in asp.net from client side java script

I have a count down timer in javascript. Once the timer is 0 seconds, I want the page to run the code in button1_click event handler.

Scenario: I am doing a quiz engine. When the time finishes, the quiz responses has to be submitted for which I want to run the button1_click event handler.

Please give me some ideas.

The simplest, albeit not necessarily the most correct, way is to simply use ASP.NET's JS function for submitting the web form:

<script type="text/javascript">
    function timerRunOut() {
        __doPostBack('id_of_button','');
    }
</script>

You can get the ID of the button by doing:

__doPostBack('<%= myButton.ClientID %>', '');

For more details, here's a good article on Understanding the JavaScript __doPostBack Function .

document.getElementById("<%= myButton.ClientID %>").onclick();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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