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...

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:

<html>
<head>
<title>JavaScript Sample by tgreer</title>
<script type="text/javascript">
  function printMe()
  {
     window.print();
     document.getElementById("printed").value = "YES";
     document.getElementById("myForm").submit();
  }
</script>
</head>
<body>
  <form id="myForm">
    <input type="button" value="Print me" onclick="printMe();" />
    <input type="hidden" id="printed" />
  </form>
</body>
</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:

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.

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?

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.

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:

<%@ Page Language="vb" AutoEventWireup="false" Codebehind="WebFormPrintControl.aspx.vb" Inherits="BarCode.WebFormPrintControl" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
	<HEAD>
		<title>WebFormPrintControl</title>
		<meta content="Microsoft Visual Studio.NET 7.0" name="GENERATOR">
		<meta content="Visual Basic 7.0" name="CODE_LANGUAGE">
		<meta content="JavaScript" name="vs_defaultClientScript">
		<meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema">
		<SCRIPT language="javascript" id="clientEventHandlersJS">
		<!--

		
		function printPage(){
		
		  var win = parent.frames[0]
		  win.focus()
		  win.print()

		  return false
		
		}
		
		function printMe()
  {
     window.print();
     document.getElementById("printed").value = "YES";
     document.getElementById("myForm").submit();
  }
		//-->
		</SCRIPT>
	</HEAD>
	<body bgColor="#ceefff" MS_POSITIONING="GridLayout">
		<form id="Form1" method="post" target="_parent" runat="server">
			<INPUT style="Z-INDEX: 100; LEFT: 232px; WIDTH: 42px; POSITION: absolute; TOP: 12px; HEIGHT: 24px"
				onclick="printPage()" type="button" value="Print"> <input type="button" value="Print me" onclick="printMe();" id="Button1" name="Button1"
				runat="server"> <input type="hidden" id="printed" style="WIDTH: 118px; HEIGHT: 22px" size="14">
			<HR style="Z-INDEX: 102; LEFT: 10px; POSITION: absolute; TOP: 5px" width="100%" color="#000099"
				SIZE="2">
			<asp:button id="ButtonCloses" style="Z-INDEX: 103; LEFT: 404px; POSITION: absolute; TOP: 12px"
				runat="server" Text="Close" Width="48px"></asp:button><INPUT style="Z-INDEX: 104; LEFT: 303px; POSITION: absolute; TOP: 13px" onclick="history.back()"
				type="button" value="<--Back">
		</form>
	</body>
</HTML>

Here is my code on my vb page.

Private Sub Button1_ServerClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.ServerClick
        Session("diditprint") = Request.Form.Item("printed")

        Me.Button1.Attributes.Add("onclick", "printMe();")
        '        Me.Button1.Attributes.Add("onclick", "printMe();")
        If Session("diditprint") = "YES" Then
           write to audit file...
        Else
            do not write to audit file.
        End If

    End Sub

I do have the runat="server" What am I doing wong?

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.

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.

Private Sub Button1_ServerClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.ServerClick
        Session("diditprint") = Request.Form.Item("printed")

        Me.Button1.Attributes.Add("onclick", "printMe();")
        '        Me.Button1.Attributes.Add("onclick", "printMe();")
        If Session("diditprint") = "YES" Then
           write to audit file...
        Else
            do not write to audit file.
        End If

    End Sub

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.

tgreer has asked me to help you out, and I will, but not till tomorrow night. Migraine has hit.

Night all

Thank you Tgreer and Paladine. I figured it out... Here is the code that worked.

Code on my html side

<INPUT id="Button3" style="Z-INDEX: 105; LEFT: 148px; POSITION: absolute; TOP: 16px" onclick="window.print();" type="button" value="Print" name="Button3" runat="server">

Sub Print()
	OLECMDID_PRINT = 6
	OLECMDEXECOPT_DONTPROMPTUSER = 2
	OLECMDEXECOPT_PROMPTUSER = 1
	On Error Resume Next
	parent.main.focus() 'seting focus on data frame
	call WB.ExecWB(6,2,1) '<--the "1" at the end will send to printer without a printer diag box showing up.
            document.Form1.printed.value = "YES"
end sub

Code on my aspx.vb side

If Request.Form("printed") = "YES" Then
   RunAudit
end if

Thanks for all the help!

Glad to hear it.

Sorry i didn't help much!

It's okay... Trying to figure this one out also gave me a Migraine.

Thanks again guys.

plz any one help i want to control Potriat and Landscape without clicking on print dialog box in javascript letme know!!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.