Printing using Web Control Print button VB.NET

Please support our JavaScript / DHTML / AJAX advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
Thread Solved

Join Date: Jul 2005
Posts: 10
Reputation: Partydude4 is an unknown quantity at this point 
Solved Threads: 0
Partydude4 Partydude4 is offline Offline
Newbie Poster

Printing using Web Control Print button VB.NET

 
0
  #1
Jul 6th, 2005
Can anyone tell me how or what's the JavaScript code to perform the following:

1. I have two frames on my web page. (Top and Bottom)
2. Top Frame is data
3. Bottom Frame are some Controls. (Print button, Back Button, Close Button.)
4. I'm using VB.Net creating aspx pages.

Question: I would like to write to a field in my sql database when the user presses the "Print" button. I was told that I needed to do the following:

"create a "Print" button, and USING JAVASCRIPT, invoke the "window.print()" method when a user presses it, then you want to author a hidden form variable, then submit the form. Your code-behind page would read the hidden form variable to know that the user clicked your "Print" button."

What will I need to perform the above? I'm very new to web development and Javascript. Please help.

Thanks...
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 1,655
Reputation: tgreer is an unknown quantity at this point 
Solved Threads: 35
Team Colleague
tgreer tgreer is offline Offline
Made Her Cry

Re: Printing using Web Control Print button VB.NET

 
0
  #2
Jul 6th, 2005
First, your print button can be an ASP.NET Web Server control, or not. Thinking it over, it might be good to use one, since then you can run both client and server-side code for it.

Let's talk about client-side first. You need to accomplish two things:

1) Open a print dialog box when the button is clicked.
2) Pass a value back to the server code that indicates the user clicked that button.

You accomplish the first task using the JavaScript window.print() method. You accompish the second task by setting the value of a hidden form element. You can do both tasks in a single JavaScript function. Here's a first attempt at what the code might look like:

JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. <html>
  2. <head>
  3. <title>JavaScript Sample by tgreer</title>
  4. <script type="text/javascript">
  5. function printMe()
  6. {
  7. window.print();
  8. document.getElementById("printed").value = "YES";
  9. document.getElementById("myForm").submit();
  10. }
  11. </script>
  12. </head>
  13. <body>
  14. <form id="myForm">
  15. <input type="button" value="Print me" onclick="printMe();" />
  16. <input type="hidden" id="printed" />
  17. </form>
  18. </body>
  19. </html>

Now, in any traditional web development system, that would be pretty much it. The user would click the button, which would fire the script. The script opens the print dialog box, sets a hidden form element value, and submits the form.

The server code would parse the Request object to see if "printed=YES", and if so, do whatever it is you want to do.

ASP.NET is NOT traditional web development, however. For one, your "onclick" code for the button control refers to server code. That would be your audit code that checked for "printed=YES".

To get a client-side click event registered to a server control, you have to do:

JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. myServerControl.Attributes.Add("onclick","printMe();");

I don't know VB.NET, that's C# syntax above. I assume it's very similar.

I hope that helps. Questions about the client-side portion you can ask in this forum. For more details on the server-side code, post in the ASP.NET forum.
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 10
Reputation: Partydude4 is an unknown quantity at this point 
Solved Threads: 0
Partydude4 Partydude4 is offline Offline
Newbie Poster

Re: Printing using Web Control Print button VB.NET

 
0
  #3
Jul 6th, 2005
Thanks for your assistance... On the server side how do capture the "Printed Value"? is it a session value? Like --> If session("Printed") = "Yes" then ... or how do I capture the value of the javascrpt?

I'm also getting the following error message:
Compiler Error Message: BC30456 'printMe' is not a member of 'ASP.WebFormPrintControl_aspx' line 35

LINE 35: <asp:button id="ButtonPrint2" style="Z-INDEX: 105; LEFT: 161px; POSITION: absolute; TOP: 12px"
onclick="printMe()" runat="server" Text="Print2"></asp:button>

Is it because I'm using a webcontrol?
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 1,655
Reputation: tgreer is an unknown quantity at this point 
Solved Threads: 35
Team Colleague
tgreer tgreer is offline Offline
Made Her Cry

Re: Printing using Web Control Print button VB.NET

 
0
  #4
Jul 6th, 2005
Your error is because, in ASP.NET-world, the "onclick" attribute points to SERVER code. So the compiler is looking for a "printMe()" method in your code-behind page. In a way, then, yes - it's because you're using a web control.

That's why I wrote previously:

ASP.NET is NOT traditional web development, however. For one, your "onclick" code for the button control refers to server code. That would be your audit code that checked for "printed=YES".

To get a client-side click event registered to a server control, you have to do:

Code:

myServerControl.Attributes.Add("onclick","printMe();");
Now, to get the hidden form element's value, you can either 1) turn it into a server object by adding the runat=server attribute to the element's declaration, or 2) use the ASP.NET Request object.
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 10
Reputation: Partydude4 is an unknown quantity at this point 
Solved Threads: 0
Partydude4 Partydude4 is offline Offline
Newbie Poster

Re: Printing using Web Control Print button VB.NET

 
0
  #5
Jul 6th, 2005
Now I'm getting an error on my webpage saying:

Line: 28
Char: 6
Error: 'document.getElementById(...)' is null or not an object
code:0
url: http://Localhost/reports/WebFormPrintControl.aspx

Here's my code on the html side:

JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. <%@ Page Language="vb" AutoEventWireup="false" Codebehind="WebFormPrintControl.aspx.vb" Inherits="BarCode.WebFormPrintControl" %>
  2. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
  3. <HTML>
  4. <HEAD>
  5. <title>WebFormPrintControl</title>
  6. <meta content="Microsoft Visual Studio.NET 7.0" name="GENERATOR">
  7. <meta content="Visual Basic 7.0" name="CODE_LANGUAGE">
  8. <meta content="JavaScript" name="vs_defaultClientScript">
  9. <meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema">
  10. <SCRIPT language="javascript" id="clientEventHandlersJS">
  11. <!--
  12.  
  13.  
  14. function printPage(){
  15.  
  16. var win = parent.frames[0]
  17. win.focus()
  18. win.print()
  19.  
  20. return false
  21.  
  22. }
  23.  
  24. function printMe()
  25. {
  26. window.print();
  27. document.getElementById("printed").value = "YES";
  28. document.getElementById("myForm").submit();
  29. }
  30. //-->
  31. </SCRIPT>
  32. </HEAD>
  33. <body bgColor="#ceefff" MS_POSITIONING="GridLayout">
  34. <form id="Form1" method="post" target="_parent" runat="server">
  35. <INPUT style="Z-INDEX: 100; LEFT: 232px; WIDTH: 42px; POSITION: absolute; TOP: 12px; HEIGHT: 24px"
  36. onclick="printPage()" type="button" value="Print"> <input type="button" value="Print me" onclick="printMe();" id="Button1" name="Button1"
  37. runat="server"> <input type="hidden" id="printed" style="WIDTH: 118px; HEIGHT: 22px" size="14">
  38. <HR style="Z-INDEX: 102; LEFT: 10px; POSITION: absolute; TOP: 5px" width="100%" color="#000099"
  39. SIZE="2">
  40. <asp:button id="ButtonCloses" style="Z-INDEX: 103; LEFT: 404px; POSITION: absolute; TOP: 12px"
  41. runat="server" Text="Close" Width="48px"></asp:button><INPUT style="Z-INDEX: 104; LEFT: 303px; POSITION: absolute; TOP: 13px" onclick="history.back()"
  42. type="button" value="<--Back">
  43. </form>
  44. </body>
  45. </HTML>

Here is my code on my vb page.

JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. Private Sub Button1_ServerClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.ServerClick
  2. Session("diditprint") = Request.Form.Item("printed")
  3.  
  4. Me.Button1.Attributes.Add("onclick", "printMe();")
  5. ' Me.Button1.Attributes.Add("onclick", "printMe();")
  6. If Session("diditprint") = "YES" Then
  7. write to audit file...
  8. Else
  9. do not write to audit file.
  10. End If
  11.  
  12. End Sub
  13.  

I do have the runat="server" What am I doing wong?
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 1,655
Reputation: tgreer is an unknown quantity at this point 
Solved Threads: 35
Team Colleague
tgreer tgreer is offline Offline
Made Her Cry

Re: Printing using Web Control Print button VB.NET

 
0
  #6
Jul 6th, 2005
Make sure you use "Form1" instead of "myForm", for one thing. I don't see why you need two buttons. Or, why you need to set a Session variable. But that's all beside the point: it's saying it doesn't recognize "document.getElementById()"??

That is truly strange.

Take the "onlick=printMe();" out of your element declaration.
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 10
Reputation: Partydude4 is an unknown quantity at this point 
Solved Threads: 0
Partydude4 Partydude4 is offline Offline
Newbie Poster

Re: Printing using Web Control Print button VB.NET

 
0
  #7
Jul 6th, 2005
Okay I changed it to Form1.

I have two buttons because the "Print" button was the orginal button that existed on this page. (BTW... I'm changing a web site that someone else created one year ago) This works fine. Now my boss wants a record of who printed this web page.

The second button (Your Code) is the "Test" button. I don't want to mess up the original button so I put a second one there to do my testing.

By the way... The code that I posted earlier was from my WebFormPrintControl.aspx.vb file It was not from my html section. Here's the code again that's on my WebFormPrintControl.aspx.vb.
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. Private Sub Button1_ServerClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.ServerClick
  2. Session("diditprint") = Request.Form.Item("printed")
  3.  
  4. Me.Button1.Attributes.Add("onclick", "printMe();")
  5. ' Me.Button1.Attributes.Add("onclick", "printMe();")
  6. If Session("diditprint") = "YES" Then
  7. write to audit file...
  8. Else
  9. do not write to audit file.
  10. End If
  11.  
  12. End Sub
  13.  

I made the button to "Run as server Control" Is this okay to do?
I want to use your code you gave me, the one with the "onclick=printMe();" Do you still want me to Delete it?

Hey... I just thought of something that I might not have told you, I'm trying to retrieve the value of "Printed" from within the WebFormPrintControl.aspx.vb page.

The reason I'm using Sessions because I have other values stored that When the user has printed the page, it will write to my sql database the user name, time and date of what page they printed out.
Reply With Quote Quick reply to this message  
Join Date: Feb 2003
Posts: 793
Reputation: Paladine has a spectacular aura about Paladine has a spectacular aura about Paladine has a spectacular aura about 
Solved Threads: 27
Team Colleague
Paladine's Avatar
Paladine Paladine is offline Offline
Master Poster

Re: Printing using Web Control Print button VB.NET

 
0
  #8
Jul 8th, 2005
tgreer has asked me to help you out, and I will, but not till tomorrow night. Migraine has hit.

Night all
Assistant Manager, Pharmacy Informatics
Wordpress Learning Blog
Updated : ASP.Net Login Code
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 10
Reputation: Partydude4 is an unknown quantity at this point 
Solved Threads: 0
Partydude4 Partydude4 is offline Offline
Newbie Poster

Re: Printing using Web Control Print button VB.NET

 
0
  #9
Jul 8th, 2005
Thank you Tgreer and Paladine. I figured it out... Here is the code that worked.

Code on my html side
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. <INPUT id="Button3" style="Z-INDEX: 105; LEFT: 148px; POSITION: absolute; TOP: 16px" onclick="window.print();" type="button" value="Print" name="Button3" runat="server">
  2.  
  3. Sub Print()
  4. OLECMDID_PRINT = 6
  5. OLECMDEXECOPT_DONTPROMPTUSER = 2
  6. OLECMDEXECOPT_PROMPTUSER = 1
  7. On Error Resume Next
  8. parent.main.focus() 'seting focus on data frame
  9. call WB.ExecWB(6,2,1) '<--the "1" at the end will send to printer without a printer diag box showing up.
  10. document.Form1.printed.value = "YES"
  11. end sub

Code on my aspx.vb side
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. If Request.Form("printed") = "YES" Then
  2. RunAudit
  3. end if

Thanks for all the help!
Reply With Quote Quick reply to this message  
Join Date: Feb 2003
Posts: 793
Reputation: Paladine has a spectacular aura about Paladine has a spectacular aura about Paladine has a spectacular aura about 
Solved Threads: 27
Team Colleague
Paladine's Avatar
Paladine Paladine is offline Offline
Master Poster

Re: Printing using Web Control Print button VB.NET

 
0
  #10
Jul 8th, 2005
Glad to hear it.

Sorry i didn't help much!
Assistant Manager, Pharmacy Informatics
Wordpress Learning Blog
Updated : ASP.Net Login Code
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the JavaScript / DHTML / AJAX Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC