views:

96

answers:

2

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.

A: 

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.

Rex M
I tried doing a postback but it just re loads the page. I want the event handler to be triggered. Any idea how? Thanks.
beginner
@unknown if you target the ID of the button which has the OnClick event handler assigned, it will invoke that method.
Rex M
A: 
document.getElementById("<%= myButton.ClientID %>").onclick();
ChaosPandion
When I use the above method , I get a script error saying object is not supported... If this could work, it would be so easy... Where I am wrong? Thanks..
beginner