window.onload = function ConfirmRetrieve() {
var retrieve = confirm('Lock this element for update?');
if (retrieve)
document.getElementById('<%=btnRetrieveHidden.ClientID%>').click();
}
It will trigger this function whenever the page loads. The thing is that I don't want to trigger this until I click some button and in that click event I'll do some checks. If certain conditions are met, I'm calling ConfirmRetrieve from the serverside.
Dim sb As New StringBuilder
sb.Append("window.onload=function (){")
sb.Append("var input = confirm('Lock this element for update?');")
sb.Append("if (input) {document.getElementById('" & btnRetrieveHidden.ClientID & "').click();}")
sb.Append("}")
Page.ClientScript.RegisterStartupScript(Me.GetType, Guid.NewGuid().ToString(), sb.ToString, True)
jameskoo87
Member
4 Points
32 Posts
Re: How to use a set timeout in javascript function
Nov 29, 2012 03:53 AM|LINK
Hi asteranup,
Thanks for the reply.
The reason I don't want to do that is that I want to trigger that function from the server side, not at the page load
asteranup
All-Star
30184 Points
4906 Posts
Re: How to use a set timeout in javascript function
Nov 29, 2012 07:05 AM|LINK
Hi,
You can call the same code from server side. It will execute properly.
Anup Das Gupta
Mark as Answer if you feel so. Visit My Blog
Raigad
Contributor
5129 Points
956 Posts
Re: How to use a set timeout in javascript function
Nov 29, 2012 09:11 AM|LINK
Hi jameskoo87
Your setTimeout code is correct but need to verify that your function is reaching else condition.
If your else condition is not executing then how your setTimeout function will execute add alert box in else condition and try.
Mark as Answer, if the post helped you...
Visit My Blog
jameskoo87
Member
4 Points
32 Posts
Re: How to use a set timeout in javascript function
Nov 29, 2012 11:57 AM|LINK
Hi Raigad,
I know it's reaching SetTimeout else condition. I did add an alert box and it gives me a pop up after some time
jameskoo87
Member
4 Points
32 Posts
Re: How to use a set timeout in javascript function
Nov 29, 2012 01:29 PM|LINK
Hi asteranup
If I do this,
window.onload = function ConfirmRetrieve() { var retrieve = confirm('Lock this element for update?'); if (retrieve) document.getElementById('<%=btnRetrieveHidden.ClientID%>').click(); }It will trigger this function whenever the page loads. The thing is that I don't want to trigger this until I click some button and in that click event I'll do some checks. If certain conditions are met, I'm calling ConfirmRetrieve from the serverside.
asteranup
All-Star
30184 Points
4906 Posts
Re: How to use a set timeout in javascript function
Nov 30, 2012 02:06 AM|LINK
Hi,
I mean to say this-
<html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <script > function ConfirmRetrieve() { var retrieve = confirm('Lock this element for update?'); if (retrieve) document.getElementById('<%=btnRetrieveHidden.ClientID%>').click(); } </script> </head> <body> <form id="form1" runat="server"> <div> <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" /> <asp:Button ID="btnRetrieveHidden" runat="server" Text="Button" OnClick="btnRetrieveHidden_Click" /> </div> </form> </body> </html>protected void Button1_Click(object sender, EventArgs e) { ClientScript.RegisterStartupScript(Page.GetType(), "key1", "window.onload=ConfirmRetrieve;", true); } protected void btnRetrieveHidden_Click(object sender, EventArgs e) { ClientScript.RegisterStartupScript(Page.GetType(), "key", "alert('hi')", true); }Anup Das Gupta
Mark as Answer if you feel so. Visit My Blog
Raigad
Contributor
5129 Points
956 Posts
Re: How to use a set timeout in javascript function
Nov 30, 2012 05:14 AM|LINK
Increase delay to 3000 or 4000 and then try.
Mark as Answer, if the post helped you...
Visit My Blog
jameskoo87
Member
4 Points
32 Posts
Re: How to use a set timeout in javascript function
Nov 30, 2012 02:11 PM|LINK
Thanks guys,
Based on your inputs, I think I'm pretty close to a solution.
I changed my code slightly, and this is so far working - without page onload
Dim sb As New StringBuilder sb.Append("function showConfirm(msg){") sb.Append("var input = confirm(msg);") sb.Append("if (input) {document.getElementById('" & btnRetrieveHidden.ClientID & "').click();}") sb.Append("}") Page.ClientScript.RegisterStartupScript(Me.GetType, Guid.NewGuid().ToString(), sb.ToString, True) Page.ClientScript.RegisterStartupScript(Me.GetType, Guid.NewGuid().ToString(), "showConfirm('Lock this element for update?');", True)So this takes my msg string as input and prompts that message as confirm box.
Now I want to try something like this, this is not working though
sb.Append("window.onload=function showConfirm(msg){") sb.Append("var input = confirm(msg);") sb.Append("if (input) {document.getElementById('" & btnRetrieveHidden.ClientID & "').click();}") sb.Append("}") Page.ClientScript.RegisterStartupScript(Me.GetType, Guid.NewGuid().ToString(), sb.ToString, True) Page.ClientScript.RegisterStartupScript(Me.GetType, Guid.NewGuid().ToString(), "showConfirm('Lock this element for update?');", True)Hm... any ideas to get this work?
Thank you all for your help
jameskoo87
Member
4 Points
32 Posts
Re: How to use a set timeout in javascript function
Nov 30, 2012 03:01 PM|LINK
Dim sb As New StringBuilder sb.Append("window.onload=function (){") sb.Append("var input = confirm('Lock this element for update?');") sb.Append("if (input) {document.getElementById('" & btnRetrieveHidden.ClientID & "').click();}") sb.Append("}") Page.ClientScript.RegisterStartupScript(Me.GetType, Guid.NewGuid().ToString(), sb.ToString, True)This is it!