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 :
Bash
head runat="server">
title>Untitled Page/title>
script type="text/javascript">
function timedMsg()
{
var t=setTimeout("alert('good morning!')",6000);
}
script>
/head>C# code :
C#
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 :
JavaScript
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 :
C#
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..

Koteswararao MallisettiRank #2,740 · 511 reputationPosted Oct 27, 2010, 4:06 AM EDT
script.Append("timedMsg(x);"); i think this is the problem the entire parameter is treated as one variable x is also a constant string try this following code script.Append("timeMsg("+x+");");//in this x is filled with that value previous one x is treated like string .Arunkumar Emm59 reputationPosted Oct 27, 2010, 6:05 AM EDT
Suthish NairRank #92 · 22.8K reputationPosted Oct 27, 2010, 7:56 PM EDT
protected void btn1_Click(object sender, EventArgs e) { String script = String.Format("javascript:timedMsg('{0}');", txt1.Text); Page.ClientScript.RegisterClientScriptBlock(typeof(object), "JavaScriptBlock", script, true); }Koteswararao MallisettiRank #2,740 · 511 reputationPosted Oct 27, 2010, 10:37 PM EDT
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="test.aspx.cs" Inherits="test" %>
c# file:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
public partial class test : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
int x = 6000;
StringBuilder script = new StringBuilder();
script.Append("");
Page.ClientScript.RegisterClientScriptBlock(typeof(object), "JavaScriptBlock", script.ToString());
}
}
-----------------------------------------------------------------------------------------------
If this post helped you, then tick the checkbox above "Do you like this Answer"