views:

109

answers:

2

I am having a form in which a button is there on click of that button i am calling a javascript function in which i am displaying like 3.. 2.. 1 kind of effect then i am returning true.

But after showing 3 only return true is getting executed and the form is getting submitted.

How to stop form submitting before running the script.

Edit:

function jsFun() 
{ 
    timerCount(3); 
    //Calling the Timer Function. 
} 

var t; 

function timerCount(cDown) 
{ 
    if (cDown == 0) 
    { 
        clearTimeout(t); 
        return true; 
    } 

    $('#<%= mainContainer.ClientID %>').html(cDown); 
    cDown = cDown - 1; 
    t = setTimeout('timerCount(' + cDown + ')', 1000); 
} 

<asp:Button ID="btnStarts" runat="server" Text="Start" OnClientClick="return jsFun();" OnClick="btn_click" />
+1  A: 

The form will be submitted if the 'submit' button returns true.

So then the answer is return false, and then do your 'countdown' (because it's async, probably), and manually submit the form manually via 'form.submit();'

Noon Silk
actually i want to call the event handler for the button click after the execution of javascript function.How to do that ?
Just call it? Post your code and maybe we can help futher. But when you hit '1', just call the submit().
Noon Silk
As far as i know the cleanest way to do this is with a web service , i.e. when the javascript is finished executing call the web service which will execute what ever .net code you need
RC1140
A: 

I've modified the code slightly not using ASP.NET like so:

function jsFun() {
    timerCount(3);
}

var t;

function timerCount(cDown) {
    if (cDown == 0) {
     clearTimeout(t);
     return true;
    }

    document.getElementById('testbutton').value = cDown;

    cDown = cDown - 1;
    t = setTimeout('timerCount(' + cDown + ')', 1000);
}

Calling it like this:

<input id="testbutton" name="testbutton" type="button" value="Test me!" onclick="return jsFun();" />

Which works exactly as advertised! I would say there's a problem with this line:

$('#<%= mainContainer.ClientID %>').html(cDown);

Hope it helps.

Mr. Smith
then how to submit the form and run the event handler for the button control.
I would say document.myForm.submit(); right after clearTimeout(t);?
Mr. Smith
http://stackoverflow.com/questions/1286770/how-to-call-event-handler-code-from-javascript-code, this link will show you how to call a server side event or method using JS.
Mr. Smith