无法将值传递给脚本 [英] unable to pass values to the script

查看:95
本文介绍了无法将值传递给脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



基本上,我只想在特定时间在asp.net应用程序中显示一个弹出框.所以我正在使用以下代码..

aspx页面:

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 :

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




C#代码:




C# code :

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());
    }



这工作正常,但我不想将settimeout设置为固定为6000.我需要从数据库中检索值并将其分配给settimeout.所以我修改了如下代码.

aspx页面:



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 :

<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#代码:



C# code :

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..

推荐答案

示例:
C#
Example:
C#
int timeOut = 6000;
 Page.ClientScript.RegisterStartupScript(this.GetType(), "javaScript", string.Format("javascript:timedMsg(''{0}'');", timeOut), true);


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>


需要一次更改:
script.Append("timedMsg(x);");

将上面的行更改为:
script.Append("timedMsg("+ x +");");
One change needed:
script.Append("timedMsg(x);");

Change above line to:
script.Append("timedMsg("+ x +");");


这篇关于无法将值传递给脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆