2011-08-02 98 views
0

我有一個SQL數據讀者,它有一堆路徑。 我需要打開瀏覽器上的多個彈出窗口/多個選項卡。 所以,我想通過我的DataReader循環並做了ClientScript.RegisterStartupScript 但代碼完成後沒有打開......通過asp.net在javascript中打開多個彈出窗口

這裏是我的代碼:

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 

實際的鏈接是儲存在我們的網絡上的本地文件服務器...

回答

2

嘗試更改JavaScript來的語法如下:

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

如果不工作,首先嚐試creting劇本,像這樣:

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 
+0

幾乎我認爲,問題是r(「AttachmentLink」)是指像「\\ ​​myserver \ attachments \ mydoc.doc」這樣的本地服務器上的文件,所以當我這樣做時,我得到一個未找到,因爲我認爲它翻譯主機到http:// localhost:80/myserver/myfolder/mypdf.pdf – oJM86o

+0

Ahhhh ...可以解釋很多...:P –

+0

有沒有辦法解決這個問題? – oJM86o

0

這是因爲RegisterStartupScript屬性的範圍類型。你有沒有想過爲什麼你應該提供一個Type type參數作爲這種方法的第一個參數?

ASP.NET Framework使用這種類型的鍵,當你第二次嘗試添加另一個腳本與同類型相同的密鑰,那麼它根本不會添加它,以防止重複的腳本會增加您的HTTP請求(儘管某些瀏覽器會緩存第一個請求並且不會發送其他類似腳本的請求)並降低您的性能。

但是,當你說什麼都沒有發生時,即使有一次,我建議將生成的腳本複製/粘貼到Firebug中,並嘗試調試它。

+0

所以,我想我有哪些其他選擇?看到這個http://forums.asp.net/t/1230403.aspx/1顯然有人想通了,但它不適合我。 – oJM86o

+1

代碼正在更改密鑰。 ''popup「+ counter.ToString()'部分。 –

+0

@Saeed也看看第二個參數,它不一樣的關鍵。 – oJM86o