Click here to Skip to main content
15,900,378 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
I have following code

JavaScript
<script type="text/javascript">
function validate()
{
if(document.getElementById('ctl00_generalContent_hdnStatus').value=='')
return false;
else
return true;
}

var t;

function timedCount()
{
t=setTimeout("validate()",1000);
}

function callAfterTime()
{
  stopCount()
  return timedCount();
}

function stopCount()
{
clearTimeout(t);
}
</script>


I want to execute validate() function after 3000miliseconds so i edit the code and add more functions.

Below is code on .cs file:
CSS
<asp:Button ID="btnNext2" runat="server" OnClientClick="return callAfterTime();" OnClick="imbNext2_Click" />


I have a problem that, i want to execute server side code only if validate() funtion returns true.
Note:It is required to execute validate function after 1000miliseconds.


[edit]Code block added, spurious formatting removed - OriginalGriff[/edit]
Posted
Updated 4-Sep-11 21:59pm
v2
Comments
BobJanova 5-Sep-11 7:06am    
Who gave you that requirement? It seems nonsensical to have a button that causes something to happen a second later.
tanishtaman 6-Sep-11 2:51am    
Actually, in the mean time (the time after button click and java script validation function calls) there is some code which is running. According to that code validation function will give me result.
The code return by me above is dummy.
BobJanova 6-Sep-11 12:53pm    
Then submit the form in the validation response event handler.

Refer to the following discusion. Its very much in lines of your problem
Validation Before Postback Event on Masterpage Asp.net[^]
 
Share this answer
 
Comments
tanishtaman 5-Sep-11 5:01am    
I'm thankful for ur reply.
But my requirement is to execute validate function after 1000miliseconds. Then, if it returns true then only server side code should executes.
Please reply.
Could you not send an ajax request to a server-side script. So the server-side code would then execute.
 
Share this answer
 
I got solution for my problem. Thanks to all how replied.
 
Share this answer
 
Comments
André Kraak 5-Oct-11 2:51am    
If the solution you found is different from the other solutions provided it would be helpful for other to know how you solved your problem.

Please use Improve solution to describe how the problem was solved.

Thanks.
XML
Yes, My problem is solved. Here is the solution.

<input type="hidden" id="hdnAjax" value="0" />
<asp:button id="btnNext2" runat="server" onclientclick="return callAfterTime();" onclick="imbNext2_Click" xmlns:asp="#unknown" />
<div style="display: none;">
<asp:imagebutton id="btnNextSubmit" runat="server" onclick="imbNext2_Click" xmlns:asp="#unknown" />
</div>

I have put a hidden variable and a hidden button on aspx page.
No in js file:
<script type="text/javascript">
function validate()
{
if (document.getElementById('hdnAjax').value == '0') {
                InputPromoCode();
                setTimeout('validate();', 500);
                return false;
            }

if(document.getElementById('ctl00_generalContent_hdnStatus').value=='')
return false;
else
{
document.getElementById('ctl00_generalContent_btnNextSubmit').click();
return true;
}
}

var t;

function timedCount()
{
t=setTimeout("validate()",1000);
document.getElementById('ctl00_generalContent_hdnData').value="1";
}

function callAfterTime()
{
  stopCount()
  return timedCount();
}

function stopCount()
{
clearTimeout(t);
}
</script>
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900