如何从后面的代码将值传递给javascript [英] how to pass values to javascript from code behind

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

问题描述


基本上,我只想在特定时间在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..

推荐答案

U可以看到我的文章

http://www.codeproject.com/KB/aspnet/JqueryCode.aspx [ ^ ]
U can see the my article

http://www.codeproject.com/KB/aspnet/JqueryCode.aspx[^]


function timedMsg(var x)
{
var t=setTimeout("alert('I am displayed after 3 seconds!')",x);
}



替换上面的代码



Replace the above code

function timedMsg(x)
{
var t=setTimeout("alert('I am displayed after 3 seconds!')",x);
}


请参见此处 [ ^ ].


这篇关于如何从后面的代码将值传递给javascript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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