I have a webform that I need to print via code-behind. Once the user clicks button1 I do a few other steps and one of the last ones is to print out the form. Here's what I've tried so far:
Dim oScript As String oScript = oScript & ("<script language='javascript'>") oScript = oScript & ("window.print()") oScript = oScript & ("</script>") Page.RegisterClientScriptBlock("test", oScript.ToString()) and I've tried: Response.Write("<script language=javascript>window.print() </script>")
Neither of these bring up the print dialog box on the client-side when they click button1. Is there something easy I'm missing? Or is there another completely different way that I need to do this? If you have suggestions please use vb.net or java since I'm unfamiliar with C#. Thanks!
Try RegisterStartupScript, that will run the command at the very end after the page.
It might also be a good idea to use the settimeout function (kind of like a timer to delay a procedure call) so you are calling this when the page has finished loading. The print code below will execute 1 second after it runs.
jcoehoorn: RegisterStartupScriptBlock() doesn't seem to work on webforms.
Raterus, I like the 1 second time delay but it didn't work. The page posts back but no print dialog box. Here is the entire sub for my button click event:
Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim ConnectionString2 As String = "packet size=4096;user id=sa;password=password;data source=""192.168.0.101"";persist security info=False;initial catalog=FT" Dim conn2 As New SqlConnection(ConnectionString2) Dim SqlUpdateCommand2 As New SqlCommand SqlUpdateCommand2.Connection = conn2 conn2.Open()
SqlUpdateCommand2.CommandText = "Insert into ft ( voucherid, clientid, createdby, issuedto, gallonsallowed ) Values ( @voucherid, @clientid, @createdby, @issuedto, @gallonsallowed );"
SqlUpdateCommand2.Parameters.Add("@voucherid", SqlDbType.VarChar, 255).Value = Label7.Text SqlUpdateCommand2.Parameters.Add("@clientid", SqlDbType.VarChar, 255).Value = strclientid SqlUpdateCommand2.Parameters.Add("@createdby", SqlDbType.VarChar, 255).Value = username SqlUpdateCommand2.Parameters.Add("@issuedto", SqlDbType.VarChar, 255).Value = Textbox1.Text SqlUpdateCommand2.Parameters.Add("@gallonsallowed", SqlDbType.Money, 8).Value = TextBox2.Text Try SqlUpdateCommand2.ExecuteNonQuery() Catch ex As Exception Label9.Text = ex.Message Finally conn2.Close() End Try Button1.Visible = False Button2.Visible = False Label9.Visible = False 'print screen Dim oScript As String oScript = oScript & ("<script language='javascript'>") oScript = oScript & ("setTimeout('window.print();',1000);") oScript = oScript & ("</script>") Page.RegisterStartupScript("test", oScript) 'send user back to beginning Response.Redirect("https://www.mysite.com/ft/createvoucher.aspx") End Sub
After you click the button, if you view the page source from the browser, can you find where your script rendered? Can you add an alert('test'); and see if it pops up when the page loads?
you're using Response.Redirect, which will send the user to a completely different page, so any client operations like what you're trying are never going to work. You need to put the print code on "https://www.mysite.com/ft/createvoucher.aspx"
When I view the source I do not see it rendered. I see another bit of javascript I use to set focus on a textbox, but not the window.print().
As for an alert you mean like this: Dim oScript As String oScript = oScript & ("<script language='javascript'>") oScript = oScript & ("setTimeout('window.print();',1000);") oScript = oScript & ("alert('Test');") oScript = oScript & ("</script>") Page.RegisterStartupScript("test", oScript)
The createvoucher.aspx is the page I'm working on. Once they complete their task on this page and trigger the button1 click event it saves their info to a db and prints the screen then basically reloads the page so they can do another one.
I don't think you understand what Response.Redirect is doing, it's not sending back any content to the browser, rather it's sending a specific HTTP code to tell the browser to move to another page. Anything you have done up to this point to the resulting HTML goes away.
Here's what I'd do, make Response.Redirect like this,
I must be confused. I need to print the page right after the use has clicked button1. This way the page prints and shows what they have entered into the textboxes. How would this work if the window.print script is in the page load event?
The current page before the redirect is the page I need to print. The user loads createvoucher.aspx and puts some info into 2 textboxes. Then they click button1 to send that info to the database, at that point I want to print the page off with the data still in the textboxes. If I redirect them and then print the page I lose the info in the textboxes. That's why I'm trying to do it at the end of that button1 event.
Printing happens client side. Your button click event happens server side. By the time your button click code runs, **the old page has already been discarded by the web browser**. The only way to do this is to print *before the page submits*.
Well, sort of. Button1 needs to print the page, but that needs to happen via javascript that was output when the page first loaded. If you wait until they click the button to run the server-side code that supports printing, it's too late.
And even when you call window.print(), this won't automatically print the page, the user still has to interact with a print dialog and hit "Ok" before anything is printed.
Are you ok with what you need to do here now? You've gotten good advice from jcoehoorn, printing is not something you can mix with a postback, or with a response.redirect.
Really, if you want to print the page, just write this and get them to click it! <input type="button" value="Print" onclick="window.print();" />