Javascript Printing

Scooter

Freshman
Joined
Nov 18, 2003
Messages
49
I've created a button on my web page in ASP.net using Visual Basic.Net and I want to use the button to print the page, I think I need to use Javascript, but I'm not that familiar with it other than the little bit of it that I found on the Web. I keep getting an error when I use the following code:

<%@ Page Language="vb" AutoEventWireup="false" Codebehind="WebForm1.aspx.vb" Inherits="PrintTest.WebForm1"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<title>WebForm1</title>
<meta name="GENERATOR" content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" content="Visual Basic .NET 7.1">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
<script type="text/javascript">
function printpage()
{
window.print()
}
</script>
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server" language=javascript onclick="return Form1_onclick()">
<asp:Button id="Button1" style="Z-INDEX: 101; LEFT: 72px; POSITION: absolute; TOP: 112px" runat="server"
Text="Print" Width="104px" Height="40px" OnClick="printpage()"></asp:Button>
</form>
</body>
</HTML>

Could someone tell me what I am doing wrong? :(
 
Scooter said:
I've created a button on my web page in ASP.net using Visual Basic.Net and I want to use the button to print the page, I think I need to use Javascript, but I'm not that familiar with it other than the little bit of it that I found on the Web. I keep getting an error when I use the following code:

<%@ Page Language="vb" AutoEventWireup="false" Codebehind="WebForm1.aspx.vb" Inherits="PrintTest.WebForm1"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<title>WebForm1</title>
<meta name="GENERATOR" content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" content="Visual Basic .NET 7.1">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
<script type="text/javascript">
function printpage()
{
window.print()
}
</script>
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server" language=javascript onclick="return Form1_onclick()">
<asp:Button id="Button1" style="Z-INDEX: 101; LEFT: 72px; POSITION: absolute; TOP: 112px" runat="server"
Text="Print" Width="104px" Height="40px" OnClick="printpage()"></asp:Button>
</form>
</body>
</HTML>
Could someone tell me what I am doing wrong? :(
replace
PHP:
<asp:Button id="Button1" 
				style="Z-INDEX: 101; LEFT: 72px; POSITION: absolute; 
								TOP: 112px"
				 runat="server"
				Text="Print" Width="104px" 
				Height="40px" OnClick="printpage()">
</asp:Button>
with
PHP:
<INPUT ID="Button1" 
				style="Z-INDEX: 101; LEFT: 72px; 
								WIDTH: 104px; POSITION: absolute; 
								TOP: 112px; HEIGHT: 40px"
				type="button" value="Print" 
				OnClick="printpage()">
 
Joe Mamma said:
replace
PHP:
<asp:Button id="Button1" 
				style="Z-INDEX: 101; LEFT: 72px; POSITION: absolute; 
								TOP: 112px"
				 runat="server"
				Text="Print" Width="104px" 
				Height="40px" OnClick="printpage()">
</asp:Button>
with
PHP:
<INPUT ID="Button1" 
				style="Z-INDEX: 101; LEFT: 72px; 
								WIDTH: 104px; POSITION: absolute; 
								TOP: 112px; HEIGHT: 40px"
				type="button" value="Print" 
				OnClick="printpage()">
That seemed to work, but is there a way to also have that button be a validating button (i.e., make a textbox visible property change from true to false)?
 
yes...
double click on your button and enter your event on the server side.

If the event is on the client side... add ";" after your command and enter your event function name.
 
I'm not quite sure I understand exactly what your saying, I'm still new to this. Let me explain what I would like to happen. I have a some textboxes, checkboxes and a print button. What I want to happen is, after the user clicks the Print button the program checks the textboxes to see if they have anything typed in them. The ones that are empty or unchecked I would like to change their visible properties' to False. I don't know if that would constitute server or client side, if it's client side I'm not sure what you are telling me to do with adding the ";" and entering my event function.
 
It'll be server side. Add a Click event to your button ( check in the property window under the "Lightning") - to add it simply double click in the correct event.

And then... loop on Me.Controls (this.Controls in C#) and here is the algo :
If( Control is TextBox )
if TextBox is Empty then
TextBox.Visible = false
 
Arch4ngel said:
It'll be server side. Add a Click event to your button ( check in the property window under the "Lightning") - to add it simply double click in the correct event.

And then... loop on Me.Controls (this.Controls in C#) and here is the algo :
If( Control is TextBox )
if TextBox is Empty then
TextBox.Visible = false

I've tried that it doesn't seem to work, my print button doesn't seem to work once I make it server side.
 
Arch4ngel said:
Button1.Attributes.Add( "onclick", "alert('Gwaaaa')");

doing this will add onclick event to it. If I was you... I'll put it in the Page_Load and make sur that this attribute isn't currently existing.

[edit]http://support.microsoft.com/default.aspx?scid=kb;en-us;318100[/edit]
What am I missing? Here is my VB Code:

ublic Class WebForm1
Inherits System.Web.UI.Page

#Region " Web Form Designer Generated Code "

'This call is required by the Web Form Designer.
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

End Sub
Protected WithEvents TextBox1 As System.Web.UI.WebControls.TextBox
Protected WithEvents Button2 As System.Web.UI.HtmlControls.HtmlInputButton

'NOTE: The following placeholder declaration is required by the Web Form Designer.
'Do not delete or move it.
Private designerPlaceholderDeclaration As System.Object

Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form Designer
'Do not modify it using the code editor.
InitializeComponent()
End Sub

#End Region

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here
Button2.Attributes.Add("onclick", "alert('Gwaaaa')")

End Sub
Private Sub Button2_ServerClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.ServerClick
If TextBox1.Text = "" Then
TextBox1.Visible = False
End If
End Sub

Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged

End Sub
End Class

and here is my HTML:

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

</HEAD>
<body MS_POSITIONING="GridLayout">
<form language="javascript" id="Form1" method="post" runat="server">

<asp:textbox id="TextBox1" style="Z-INDEX: 102; LEFT: 120px; POSITION: absolute; TOP: 232px"
runat="server" Width="128px"></asp:textbox><INPUT id="Button2" style="Z-INDEX: 103; LEFT: 88px; WIDTH: 120px; POSITION: absolute; TOP: 112px; HEIGHT: 40px"
type="button" value="Print" name="Button2" runat="server"></form>
</body>
</HTML>
 
Back
Top