【问题标题】:open multiple popup windows in javascript via asp.net通过asp.net在javascript中打开多个弹出窗口
【发布时间】:2011-08-02 13:31:38
【问题描述】:

我有一个 sql 数据读取器,里面有一堆路径。 我需要在浏览器上打开多个弹出窗口/多个选项卡。 所以我尝试遍历我的数据阅读器并执行 ClientScript.RegisterStartupScript 但是代码完成后什么都没有打开...

这是我的代码:

While r.Read()
            ClientScript.RegisterStartupScript(Me.GetType, "popup" + counter.ToString(), "window.open('" + CType(r("AttachmentLink"), String) + "','_blank' + new Date().getTime(),'menubar=no')", True)
            counter += 1
        End While

我放了一块手表,我的阅读器确实包含我想要的数据,但是没有弹出窗口打开:(。

编辑

这是我的数据库的 AttachmentLink 列中的一些示例数据:

\\myserver\myfolder\1.pdf
\\myserver\myfolder\mydoc.doc
\\myserver\myfolder\myimage.jpg

实际链接是存储在我们网络上的本地文件服务器...

【问题讨论】:

    标签: javascript asp.net vb.net sqldatareader


    【解决方案1】:

    尝试将 javascript 更改为以下语法:

    window.open(url, '_blank', 'menubar=no')
    

    如果这不起作用,请先尝试创建脚本,如下所示:

    Dim sb as New StringBuilder()
    Do While r.Read()
        sb.AppendLine("window.open('" & r("AttachmentLink") & "', '_blank', 'menubar=no');")
    Loop
    ClientScript.RegisterStartupScript(Me.GetType(), "popup", sb.ToString(), True)
    

    我还注意到一件事是您在 javascript 代码中遗漏了一个分号,有时它会搞砸事情。

    编辑添加

    回答评论,您可以使用以下内容:

    sb.AppendLine("window.open('" & LoadPageLink(r("AttachmentLink")) & "' ... )")
    
    Function LoadPageLink(path As String) As String
        Return String.Format("loadFile.aspx?p={0}", Server.UrlEncode(path))
    End Function
    
    ----- LoadFile.aspx
    
    Sub Page_Load(sender as Object, e as EventArgs)
        '*
        '* OK The worst part here is to detect the content-type of the file
        '* because it is being served by a proxy page, instead of directing 
        '* the browser to the actual file, which would make the IIS gess the
        '* content type and send the correct one. 
        '* 
        '* Getting the correct content type is beyond the scope of this answer
        '*
    
        Dim buffer as Byte(1024)
    
        Using (stream as New FileStream(Request("p")))
            Do While True
               Dim read = stream.Read(buffer, 0, buffer.Length)
               If (read > 0) Then
                   Response.OutputStream.Write(buffer, 0, read)
               Else
                   Exit Do
               End If
            End Do
        End Using
    
    End Sub
    

    【讨论】:

    • 几乎我认为,问题是 r("AttachmentLink") 指的是本地服务器上的一个文件,如 "\\myserver\attachments\mydoc.doc" 所以当我这样做时,我得到一个 not发现是因为我认为它将主机翻译为localhost:80/myserver/myfolder/mypdf.pdf
    • 啊啊啊...这可以解释很多... :P
    • 有什么办法吗?
    • 嗯,您需要更改数据库中的位置,使其指向正确的位置,或者您可以编写一个包装器。稍后我会添加类似的内容
    • 但它是一个合适的地方,它是我们文件服务器上的一个文件。
    【解决方案2】:

    这是因为RegisterStartupScript 的作用域是类型。你有没有想过为什么要提供一个Type type 参数作为这个方法的第一个参数?

    ASP.NET Framework 使用此类型作为键,当您第二次尝试添加具有 相同类型相同键 的另一个脚本时,它根本不会添加它,以防止重复的脚本增加您的 HTTP 请求(尽管某些浏览器会缓存第一个请求并且不发送其他类似脚本的请求)并降低您的性能。

    但是,当您说实际上什么都没有发生时,我建议您将生成的脚本复制/粘贴到 Firebug 中并尝试对其进行调试。

    【讨论】:

    • 所以我猜我还有什么其他选择?看到这个forums.asp.net/t/1230403.aspx/1 显然有人想出来了,但这对我来说不起作用。
    • 代码正在更改密钥。 "popup" + counter.ToString() 部分。
    • @Saeed 也看第二个参数,不是同一个key。
    猜你喜欢
    • 2011-02-16
    • 2015-10-31
    • 2023-02-26
    • 2010-11-05
    • 1970-01-01
    • 2018-02-27
    • 2013-06-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多