0

I had to run some server side code to open multiple files via javascript something to this effect:

Assume r is just a reader with some data:

If r IsNot Nothing Then
    Dim sb As New StringBuilder()
    Do While r.Read()
        'added below to replace \\ with http://
        strURL = Replace(CType(r("AttachmentLink"), String), "\\myServer\MyFolder\MyPath", "http://MyFolder/MyPath", , , CompareMethod.Text)
        'added below to replace \ with /
        strURL = Replace(strURL, "\", "/")
        sb.AppendLine("window.open('" & strURL & "', '_blank', 'menubar=no');")
    Loop
    ClientScript.RegisterStartupScript(Me.GetType(), "popup", sb.ToString(), True)
End If

This works great for opening multiple attachments... but now I need to not only open them up but print them...

So I tried just taking what I had above and modifying a little:

Do While r.Read()
    'added below to replace \\ with http://
    strURL = Replace(CType(r("AttachmentLink"), String), "\\myServer\MyFolder\MyPath", "http://MyFolder/MyPath", , , CompareMethod.Text)
    'added below to replace \ with /
    strURL = Replace(strURL, "\", "/")
    sb.AppendLine("var oWindow = window.open('" & strURL & "', '_blank', 'menubar=no');")
    sb.AppendLine("oWindow.print();")
    sb.AppendLine("oWindow.close();")
Loop
ClientScript.RegisterStartupScript(Me.GetType(), "popup", sb.ToString(), True)

This of course does not work, no error but nothing comes up. I was hoping to get each window open up and a print dialog from javascript to pop up...

Any ideas?

Robert Koritnik
  • 103,639
  • 52
  • 277
  • 404
oJM86o
  • 2,108
  • 8
  • 43
  • 71

1 Answers1

0

Due to browser javascript security reasons you're not allowed to print a child window.

What you could do is add a load script into those pages you'd like to print that will initiate printing from that child window instead.

Check this answer that shows a bit different aproach in a sense that:

  1. it creates an iframe
  2. loads some content in it and also
  3. adds onload event to print it...
Community
  • 1
  • 1
Robert Koritnik
  • 103,639
  • 52
  • 277
  • 404
  • Errr...is there any way around this because I've seen people post stuff like this: http://stackoverflow.com/questions/7187400/printing-multiple-pdf-files-using-javascript – oJM86o Sep 13 '11 at 13:05
  • I dont know how to handle this in my situation :( – oJM86o Sep 13 '11 at 14:06