Calling window.print from server page

D

Diego F.

Hello. I'm trying to call a function that prints the content of a div
element. I had that javascript code working, but now I must put that code in
the server side, as it is called from another component.

The problem is that it can't find the div control. Don't know why. I paste
the code here:

That's in the aspx.vb file:

Response.Write("<script>")
Response.Write(" var prtContent = document.getElementById(divPrint);")
Response.Write("var WinPrint =
window.open('','','letf=0,top=0,width=1,height=1,toolbar=0,scrollbars=0,status=0');")
Response.Write(" WinPrint.document.write(prtContent.innerHTML);")
Response.Write(" WinPrint.document.close();")
Response.Write(" WinPrint.focus();")
Response.Write(" WinPrint.print();")
Response.Write(" WinPrint.close();")
Response.Write(" prtContent.innerHTML=strOldOne;")
Response.Write("</script>")

In the aspx file there is the div control with a datagrid inside.
 
M

Mythran

Diego F. said:
Hello. I'm trying to call a function that prints the content of a div
element. I had that javascript code working, but now I must put that code
in the server side, as it is called from another component.

The problem is that it can't find the div control. Don't know why. I paste
the code here:

That's in the aspx.vb file:

Response.Write("<script>")
Response.Write(" var prtContent = document.getElementById(divPrint);")
Response.Write("var WinPrint =
window.open('','','letf=0,top=0,width=1,height=1,toolbar=0,scrollbars=0,status=0');")
Response.Write(" WinPrint.document.write(prtContent.innerHTML);")
Response.Write(" WinPrint.document.close();")
Response.Write(" WinPrint.focus();")
Response.Write(" WinPrint.print();")
Response.Write(" WinPrint.close();")
Response.Write(" prtContent.innerHTML=strOldOne;")
Response.Write("</script>")

In the aspx file there is the div control with a datagrid inside.

Replace:
Response.Write(" var prtContent = document.getElementById(divPrint);")

With:
Response.Write(" var prtContent = document.getElementById('divPrint');")


HTH,
Mythran
 
D

Diego F.

Replace:
Response.Write(" var prtContent = document.getElementById(divPrint);")

With:
Response.Write(" var prtContent = document.getElementById('divPrint');")

It was a mistake pasting the code. It's written as you mention.
 
M

Mythran

Diego F. said:
Hello. I'm trying to call a function that prints the content of a div
element. I had that javascript code working, but now I must put that code
in the server side, as it is called from another component.

The problem is that it can't find the div control. Don't know why. I paste
the code here:

That's in the aspx.vb file:

Response.Write("<script>")
Response.Write(" var prtContent = document.getElementById(divPrint);")
Response.Write("var WinPrint =
window.open('','','letf=0,top=0,width=1,height=1,toolbar=0,scrollbars=0,status=0');")
Response.Write(" WinPrint.document.write(prtContent.innerHTML);")
Response.Write(" WinPrint.document.close();")
Response.Write(" WinPrint.focus();")
Response.Write(" WinPrint.print();")
Response.Write(" WinPrint.close();")
Response.Write(" prtContent.innerHTML=strOldOne;")
Response.Write("</script>")

In the aspx file there is the div control with a datagrid inside.

Try:

Dim script As String = _
"<script language=""javascript"" type=""text/javascript"">" & vbNewLine
& _
" function WindowPrint() {" & vbNewLine & _
" var prtContent = document.getElementById('divPrint');" & vbNewLine
& _
" var WinPrint = window.open(" & _
"'', '',
'letf=0,top=0,width=1,height=1,toolbar=0,scrollbars=0,status=0');" &
vbNewLine & _
" WinPrint.document.write(prtContent.innerHTML);" & vbNewLine & _
" WinPrint.document.close();" & vbNewLine & _
" WinPrint.focus();" & vbNewLine & _
" WinPrint.print();" & vbNewLine & _
" WinPrint.close();" & vbNewLine & _
" prtContent.innerHTML=strOldOne;" & _
" }" & vbNewLine

Page.RegisterScript("WindowPrint", script)

Then, in your Page_Render, have:

Page.RegisterStartupScript("DoWindowPrint", _
"<script language=""javascript"" type=""text/javascript"">" & vbNewLine
& _
" WindowPrint();" & vbNewLine & _
"</script>" _
)

Hope this works right, off top of head...

Mythran
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top