如何使用RegisterStartupScript调用jquery对话框?

时间:2014-05-09 22:14:46

标签: jquery asp.net vb.net

我有以下vb.net并且它运行良好

Public Shared Sub createAlert(ByVal page As Page, ByVal msg As String)
    msg = msg.Replace(vbCrLf, "\n").Replace("'", "\'")
    page.ClientScript.RegisterStartupScript(page.GetType, "clientScript", "<script language='javascript'>alert('" & msg & "');</script>")
End Sub

现在我想实现这个http://jsfiddle.net/WrdM9/2/。它是一个jquery对话框,3秒后自动关闭

我试过以下没有运气 我究竟做错了什么?

Public Shared Sub AlertAutoClose(ByVal page As Page, ByVal msg As String)
    Dim sScript As String
    sScript = "<script src=""js/jquery.js"">$(document).ready(function(){$('#dialog').html('some message');});"
    sScript += "$(document).ready(function(){$('#dialog').dialog({"
    sScript += "autoOpen: true,"
    sScript += "show: ""blind"","
    sScript += "hide: ""explode"","
    sScript += "modal: true,"
    sScript += "open: function(event, ui) {"
    sScript += "setTimeout(function(){"
    sScript += "$('#dialog').dialog('close');                "
    sScript += "}, 3000);"
    sScript += "}"
    sScript += "});});</script>"

    msg = msg.Replace(vbCrLf, "\n").Replace("'", "\'")
    page.ClientScript.RegisterStartupScript(page.GetType, "Script", sScript)
End Sub

3 个答案:

答案 0 :(得分:1)

当您在脚本标记中引用脚本文件时,如src =&#34; js / jquery.js&#34;,脚本标记之间的内容将不会被执行,并且它在技术上是非法。因此,您只需要将其分解为单独的脚本标记。这将有效:

<script src="js/jQuery.js"></script>
<script type="text/javascript">
    alert('test');
</script> 

这不会:

<script src="js/jQuery.js">
    alert('test');
</script> 

答案 1 :(得分:0)

我认为第一个问题是你输入的内容有2个双引号。我认为它需要

<script src=\"js/jquery.js\"></script><script type=\"text/javascript\">$(document).ready(function(){$....

确保您也传递了Jquery文件的正确路径。

您还可以通过在aspx / html页面标记上添加jquery脚本来排除jquery。你会把你的字符串改为

<script type=\"text/javascript\">$(document).ready(function(){$

答案 2 :(得分:0)

我将代码更改为此。它现在正在运作

sScript = "<script type=""text/javascript"" src=""/js/jquery-ui-1.10.4.min.js""></script>"
sScript += "<script type=""text/javascript"" language=""javascript"">$('#dialog1').html('" & msg & "');"
sScript += "$('#dialog1').dialog({"
sScript += "autoOpen: true,"
sScript += "show: ""blind"","
sScript += "hide: ""explode"","
sScript += "modal: true,"
sScript += "open: function(event, ui) {"
sScript += "setTimeout(function(){"
sScript += "$('#dialog1').dialog('close');"
sScript += "}, 3000);"
sScript += "}"
sScript += "});</script>"
msg = msg.Replace(vbCrLf, "\n").Replace("'", "\'")
page.ClientScript.RegisterStartupScript(page.GetType, "Script", sScript)