Advertisement

05.09.2008 at 06:56AM PDT, ID: 23389299 | Points: 500
[x]
Attachment Details
How do I print a webform?
Hello Experts

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!
Start your free trial to view this solution
Question Stats
Zone: Programming
Question Asked By: jay-are
Question Asked On: 05.09.2008
Participating Experts: 2
Points: 500
Views: 0
Translate:
Loading Advertisement...
05.09.2008 at 07:06AM PDT, ID: 21532933

Rank: Guru

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
05.09.2008 at 07:08AM PDT, ID: 21532956

Rank: Genius

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
05.09.2008 at 07:21AM PDT, ID: 21533093

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
05.09.2008 at 07:26AM PDT, ID: 21533140

Rank: Guru

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
05.09.2008 at 07:31AM PDT, ID: 21533181

Rank: Genius

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
05.09.2008 at 07:33AM PDT, ID: 21533207

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
05.09.2008 at 07:34AM PDT, ID: 21533214

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
05.09.2008 at 07:41AM PDT, ID: 21533298

Rank: Genius

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
05.09.2008 at 07:54AM PDT, ID: 21533463

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
05.09.2008 at 07:58AM PDT, ID: 21533506

Rank: Guru

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
05.09.2008 at 07:58AM PDT, ID: 21533503

Rank: Genius

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
05.09.2008 at 08:07AM PDT, ID: 21533612

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
05.09.2008 at 08:15AM PDT, ID: 21533694

Rank: Guru

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
05.09.2008 at 08:17AM PDT, ID: 21533726

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
05.09.2008 at 08:23AM PDT, ID: 21533795

Rank: Guru

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
05.09.2008 at 08:35AM PDT, ID: 21533943

Rank: Genius

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
05.09.2008 at 08:41AM PDT, ID: 21533996

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
05.09.2008 at 08:48AM PDT, ID: 21534061

Rank: Genius

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 
Loading Advertisement...
Microsoft
  • Internet Protocols
  • Applications
  • Development
  • OS
  • Hardware
  • Windows Security
Apple
  • Operating Systems
  • Hardware
  • Programming
  • Networking
  • Software
Internet
  • Search Engines
  • File Sharing
  • WebTrends / Stats
  • Spy / Ad Blockers
  • Web Browsers
  • New Net Users
  • Web Development
  • Chat / IM
  • Anti Spam
  • Web Servers
  • Anti-Virus
  • Email Clients
Gamers
  • Tips
  • Online / MMORPG
  • Puzzle
  • Emulators
  • Action / Adventure
  • Role Playing
  • Consoles
  • Game Programming
  • Strategy
  • Sports
  • Misc
  • Computer Games
Digital Living
  • Hardware
  • New Net Users
  • New Users
  • Software
  • Digital Music
  • Gaming World
  • Home Security
  • Apple
  • Networking Hardware
Virus & Spyware
  • Vulnerabilities
  • IDS
  • Encryption
  • Anti-Virus
  • Operating Systems Security
  • Software Firewalls
  • WebApplications
  • Cell Phones
  • Operating Systems
  • Internet
  • Hardware Firewalls
Hardware
  • Handhelds / PDAs
  • Displays / Monitors
  • Components
  • Networking Hardware
  • Peripherals
  • Laptops/Notebooks
  • Storage
  • Servers
  • Desktops
  • New Users
  • Misc
  • Apple
Software
  • System Utilities
  • Industry Specific
  • Network Management
  • Photos / Graphics
  • Page Layout
  • VMWare
  • Misc
  • Web Development
  • OS
  • CYGWIN
  • Voice Recognition
  • Message Queue
  • Quality Assurance
  • Security
  • Firewalls
  • MultiMedia Applications
  • Development
  • Database
  • Office / Productivity
  • Business Management
  • OS/2 Apps
  • Server Software
  • Internet / Email
ITPro
  • OS
  • Storage
  • Encryption
  • Operating Systems Security
  • Apple Hardware
  • Laptops & Notebooks
  • Servers
  • Networking Hardware
  • Peripherals
  • Devices
  • Displays / Monitors
  • WebTrends / Stats
  • Search Engines
  • Firewalls
  • WebApplications
  • IDS
  • Vulnerabilities
  • Email Clients
  • File Sharing
  • Spy / Ad Blockers
  • Web Browsers
  • Web Servers
  • Networking
  • Anti-Virus
  • Chat / IM
  • Anti Spam
Developer
  • Web Servers
  • Web Browsers
  • Game Programming
  • Dev Tools
  • Industry Specific
  • Office / Productivity
  • Database
  • CYGWIN
  • Web Development
  • Search Engines
  • File Sharing
  • WebTrends / Stats
  • Programming
  • Content Management
  • Application Servers
  • Protocols
Storage
  • Removable Backup Media
  • Storage Technology
  • Servers
  • Grid
  • Remote Access
  • Backup / Restore
  • Misc
  • Hard Drives
OS
  • Miscellaneous
  • Security
  • Development
  • Linux
  • VMWare
  • MainFrame OS
  • Unix
  • Apple
  • OS / 2
  • AS / 400
  • BeOS
  • Microsoft
  • VMS / OpenVMS
Database
  • Oracle
  • Miscellaneous
  • MySQL
  • Software
  • Sybase
  • Contact Management
  • PostgreSQL
  • Data Manipulation
  • Clarion
  • InterSystems Cache
  • Siebel
  • MUMPS
  • OLAP
  • SQLBase
  • SAS
  • GIS & GPS
  • 4GL
  • Berkeley DB
  • DB2
  • Informix
  • Interbase / Firebird
  • FoxPro
  • Reporting
  • LDAP
  • Filemaker Pro
  • MS SQL Server
  • dBase
  • MS Access
Security
  • Misc
  • Web Browsers
  • Software Firewalls
  • Operating Systems Security
  • File Sharing
  • Spy / Ad Blockers
  • Vulnerabilities
  • WebApplications
  • IDS
  • Anti-Virus
  • Encryption
  • Anti Spam
  • Email Clients
  • VPN
  • Chat / IM
Programming
  • Editors IDEs
  • Installation
  • Handhelds / PDAs
  • Multimedia Programming
  • System / Kernel
  • Algorithms
  • Game
  • Signal Processing
  • Project Management
  • Open Source
  • Database
  • Misc
  • Languages
  • Processor Platforms
  • Theory
Web Development
  • Scripting
  • Blogs
  • Web Servers
  • Software
  • Search Engines
  • Web Graphics
  • Images
  • Internet Marketing
  • Images and Photos
  • Components
  • Document Imaging
  • Web Languages/Standards
  • Illustration
  • WebApplications
  • Fonts
  • WebTrends / Stats
  • Authoring
  • Digital Camera Software
  • Miscellaneous
Networking
  • Protocols
  • Apple Networking
  • Network Management
  • Message Queue
  • Application Servers
  • Content Management
  • File Servers
  • Email Servers
  • Misc
  • Java Editors & IDEs
  • Wireless
  • Networking Hardware
  • Backup / Restore
  • System Utilities
  • ISPs & Hosting
  • Web Servers
  • Storage Technology
  • Removable Backup Media
  • Servers
  • Broadband
  • Grid
  • OS / 2
  • Novell Netware
  • Unix Networking
  • Windows Networking
  • Security
  • Telecommunications
  • Operating Systems
  • Linux Networking
Other
  • Community Advisor
  • Lounge
  • Community Support
  • New Net Users
  • Philosophy / Religion
  • Math / Science
  • Miscellaneous
  • URLs
  • Expert Lounge
  • Politics
  • Puzzles / Riddles
Community Support
  • Suggestions
  • New to EE
  • New Topics
  • Community Advisor
  • CleanUp
  • Announcements
  • General
  • Feedback
  • Input
  • EE Bugs
 
05.09.2008 at 07:06AM PDT, ID: 21532933

Rank: Guru

Try RegisterStartupScriptBlock(), and put a semi-colon(;) after window.print()
 
05.09.2008 at 07:08AM PDT, ID: 21532956

Rank: Genius

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.

        oScript = oScript & ("<script language='javascript'>")
        oScript = oScript & ("setTimeout('window.print();',1000);")
        oScript = oScript & ("</script>")
        Page.RegisterStartupScript("test", oScript)
 
05.09.2008 at 07:21AM PDT, ID: 21533093
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
 
05.09.2008 at 07:26AM PDT, ID: 21533140

Rank: Guru

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?
 
05.09.2008 at 07:31AM PDT, ID: 21533181

Rank: Genius

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"
 
05.09.2008 at 07:33AM PDT, ID: 21533207
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)


If that code is correct then it didn't work.
 
05.09.2008 at 07:34AM PDT, ID: 21533214
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.
 
05.09.2008 at 07:41AM PDT, ID: 21533298

Rank: Genius

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,

Response.Redirect("https://www.mysite.com/ft/createvoucher.aspx?print=true")

Then in Page Load of CreateVoucher.aspx

if Request.QueryString("print") = "true" then
        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)
end if
 
05.09.2008 at 07:54AM PDT, ID: 21533463
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?
 
05.09.2008 at 07:58AM PDT, ID: 21533506

Rank: Guru

Because anytime you do any server action in asp.net, the page has to load again.
 
05.09.2008 at 07:58AM PDT, ID: 21533503

Rank: Genius

I was assuming the page view that you are Response.Redirecting to is the page you are interested in printing? correct?
 
05.09.2008 at 08:07AM PDT, ID: 21533612
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.
 
05.09.2008 at 08:15AM PDT, ID: 21533694

Rank: Guru

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*.
 
05.09.2008 at 08:17AM PDT, ID: 21533726
So can I just change button1 event to print then have it call another sub for updating the db & redirecting?
 
05.09.2008 at 08:23AM PDT, ID: 21533795

Rank: Guru

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.
 
05.09.2008 at 08:35AM PDT, ID: 21533943

Rank: Genius

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.
 
05.09.2008 at 08:41AM PDT, ID: 21533996
That's fine, I just need to give them the option to print.  
 
05.09.2008 at 08:48AM PDT, ID: 21534061

Rank: Genius

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();" />
 
 
20080236-EE-VQP-29 / EE_QW_2_20070628