Click here to Skip to main content
15,905,322 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi ,

Basically I want to display a popup box only at a specific time in asp.net application. so i'm using the following code..

aspx page :
XML
<head runat="server">
    <title>Untitled Page</title>
    <script type="text/javascript">
function timedMsg()
{
var t=setTimeout("alert('good morning!')",6000);
}
</script>
</head>



C# code :

XML
protected void Button1_Click(object sender, EventArgs e)
    {
        StringBuilder script = new StringBuilder();
        script.Append("<script type=\"text/javascript\">");
        script.Append("timedMsg();");
        script.Append("</script>");
        Page.ClientScript.RegisterClientScriptBlock(typeof(object), "JavaScriptBlock", script.ToString());
    }


this works fine but i dont want to set settimeout as fixed as 6000. i need to retrieve value from database and assign it to settimeout. so i modified the coding as below..

aspx page :

XML
<head runat="server">
    <title>Untitled Page</title>
    <script type="text/javascript">
function timedMsg(var x)
{
var t=setTimeout("alert('I am displayed after 3 seconds!')",x);
}
</script>
</head>


C# code :

XML
protected void Button1_Click(object sender, EventArgs e)
    {
        int x = 6000;
        StringBuilder script = new StringBuilder();
        script.Append("<script type=\"text/javascript\">");
        script.Append("timedMsg(x);");
        script.Append("</script>");
        Page.ClientScript.RegisterClientScriptBlock(typeof(object), "JavaScriptBlock", script.ToString());
    }


but this code is not working. the alert box is not popping up. is this a correct method to pass values to the script?. or what else to be done to meet the requirement?.. pls help me..

thanks..
Posted
Comments
thatraja 27-Oct-10 8:14am    
You asked your question Clearly with full details & separate code blocks. Keep continue hereafter like the same way. Happy coding

Example:
C#
int timeOut = 6000;
 Page.ClientScript.RegisterStartupScript(this.GetType(), "javaScript", string.Format("javascript:timedMsg('{0}');", timeOut), true);
 
Share this answer
 
XML
Replace var x by x<br />
<head runat="server">
    <title>Untitled Page</title>
    <script type="text/javascript">
function timedMsg(x)
{
var t=setTimeout("alert('I am displayed after 3 seconds!')",x);
}
</script>
</head>
 
Share this answer
 
One change needed:
script.Append("timedMsg(x);");

Change above line to:
script.Append("timedMsg("+ x +");");
 
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