开发者

open multiple popup windows in javascript via asp.net

I've got an sql datareader that has a bunch of paths in it. I need to open up multiple pop up windows / multiple tabs on the browser. So I tried looping through my datareader and doing a ClientScript.RegisterStartupScript but after the code completes nothing opens 开发者_StackOverflow中文版up...

Here is my code:

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

I put a watch in and my reader does contain the data I want, but no popup window opens :(.

edit

Here is some sample data that is in the AttachmentLink column of my database:

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

The actual link is to a local file server stored on our network...


Try changing the javascript to the following syntax:

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

If that doesn't work, try creting the script first, like so:

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)

One thing I noticed too is that you missed a semi-colon in the javascript code, sometimes it can mess things pretty bad.

Edited to Add

Answering a comment, you could use something like this:

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


It's because RegisterStartupScript is scoped to types. Have you ever wondered why should you provide a Type type parameters as the first parameter of this method?

ASP.NET Framework uses this type as a key and when you for the second time try to add another script with the same type and the same key, then it simply won't add it, to prevent duplicate scripts which increase your HTTP requests (though some browsers cache the first request and don't send request for other similar scripts) and reduces your performance.

However, when you say that nothing actually happens, even for one time, I suggest to copy/paste your generated script into Firebug and try to debug it.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜