Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. Pop up print window on button click

Pop up print window on button click

Scheduled Pinned Locked Moved ASP.NET
questionhtmldebuggingworkspace
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • F Offline
    F Offline
    For_IT
    wrote on last edited by
    #1

    Hi all I am implementing print configuration functionality on button click: I am using following code:

    protected void lnkButton_Click(object sender, EventArgs e)
    {

        StringBuilder sb = new StringBuilder();
        sb.AppendLine(" var popup = window.open( '','popup','toolbar=no,menubar=no,scrollbars=1,width=750,height=800');");
        sb.AppendLine("popup.document.write('debugger');");
        sb.AppendLine("popup.document.write('<html><head>');");
        sb.AppendLine("popup.document.write('<title>Configurations for ACA</title>');");
        sb.AppendLine("popup.document.write('</head><body>');");
        sb.AppendLine("popup.document.write('<div id='lnk' class='noprint' style='text-align:right'>');");
        sb.AppendLine("popup.document.write('<a href='# onclick='window.print();return false;'>Print</a>');");
        sb.AppendLine("popup.document.write('<a href='#' onclick='window.close();return false;'>Close</a>');");
        sb.AppendLine("popup.document.write('</div>');");
        sb.AppendLine("popup.document.write('<table border=1px style=width:500px; margin-left:100px; margin-top:50px; border-color:Maroon; >');");
        sb.AppendLine("popup.document.write('<tr><td>');");
        sb.AppendLine("popup.document.write('This is my string');");
        sb.AppendLine("popup.document.write('</td><td>')");
        sb.AppendLine("popup.document.write('" + MyString + @"');");
        sb.AppendLine("popup.document.write('</td></tr></table>');");
        sb.AppendLine("popup.document.write('</body></html>');");
    
        Page.ClientScript.RegisterStartupScript(this.GetType(), "OpenWindow", sb.ToString(), true);
    }
    

    when I am executing this code, I am getting an exception on line sb.AppendLine("popup.document.write('<div id='lnk' class='noprint' style='text-align:right'>');"); Can anyone please give me suggestion what is wrong? Many thanks.

    C 1 Reply Last reply
    0
    • F For_IT

      Hi all I am implementing print configuration functionality on button click: I am using following code:

      protected void lnkButton_Click(object sender, EventArgs e)
      {

          StringBuilder sb = new StringBuilder();
          sb.AppendLine(" var popup = window.open( '','popup','toolbar=no,menubar=no,scrollbars=1,width=750,height=800');");
          sb.AppendLine("popup.document.write('debugger');");
          sb.AppendLine("popup.document.write('<html><head>');");
          sb.AppendLine("popup.document.write('<title>Configurations for ACA</title>');");
          sb.AppendLine("popup.document.write('</head><body>');");
          sb.AppendLine("popup.document.write('<div id='lnk' class='noprint' style='text-align:right'>');");
          sb.AppendLine("popup.document.write('<a href='# onclick='window.print();return false;'>Print</a>');");
          sb.AppendLine("popup.document.write('<a href='#' onclick='window.close();return false;'>Close</a>');");
          sb.AppendLine("popup.document.write('</div>');");
          sb.AppendLine("popup.document.write('<table border=1px style=width:500px; margin-left:100px; margin-top:50px; border-color:Maroon; >');");
          sb.AppendLine("popup.document.write('<tr><td>');");
          sb.AppendLine("popup.document.write('This is my string');");
          sb.AppendLine("popup.document.write('</td><td>')");
          sb.AppendLine("popup.document.write('" + MyString + @"');");
          sb.AppendLine("popup.document.write('</td></tr></table>');");
          sb.AppendLine("popup.document.write('</body></html>');");
      
          Page.ClientScript.RegisterStartupScript(this.GetType(), "OpenWindow", sb.ToString(), true);
      }
      

      when I am executing this code, I am getting an exception on line sb.AppendLine("popup.document.write('<div id='lnk' class='noprint' style='text-align:right'>');"); Can anyone please give me suggestion what is wrong? Many thanks.

      C Offline
      C Offline
      Christian Graus
      wrote on last edited by
      #2

      you used single quotes for both the text, and the inner text. This won't work, you need to use double quotes for the attributes, or the string literal. sb.AppendLine("popup.document.write('<div id="lnk" class="noprint" style="text-align:right">');");

      Christian Graus Please read this if you don't understand the answer I've given you "also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )

      F 1 Reply Last reply
      0
      • C Christian Graus

        you used single quotes for both the text, and the inner text. This won't work, you need to use double quotes for the attributes, or the string literal. sb.AppendLine("popup.document.write('<div id="lnk" class="noprint" style="text-align:right">');");

        Christian Graus Please read this if you don't understand the answer I've given you "also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )

        F Offline
        F Offline
        For_IT
        wrote on last edited by
        #3

        Thank you very much, this was helpful. I have another problem now: When I click on "Print" on pop up window, it does not open Print dialog box. When I wrote the client side javascript, it does open Print dialog box with printer listed. Can anyone please what improvement should be done in the code? my code now look like

        protected void lnkButton_Click(object sender, EventArgs e)
        {
        StringBuilder sb = new StringBuilder();
        sb.AppendLine(" var popup = window.open( '','popup','toolbar=no,menubar=no,scrollbars=1,width=750,height=800');");
        sb.AppendLine("popup.document.write('<html><head>');");
        sb.AppendLine("popup.document.write('<title>Configurations for ACA</title>');");
        sb.AppendLine("popup.document.write('</head><body>');");
        sb.AppendLine("popup.document.write('<div id=\"lnk\" class=\"noprint\" style=\"text-align:right\">');");
        sb.AppendLine("popup.document.write('<a href=\"#\" onclick=\"window.print();return false;\">Print</a>');");
        sb.AppendLine("popup.document.write('<a href=\"#\" onclick=\"window.close();return false;\">Close</a>');");
        sb.AppendLine("popup.document.write('</div>');");
        sb.AppendLine("popup.document.write('<table border=1px style=width:500px; margin-left:100px; margin-top:50px; border-color:Maroon; >');");
        sb.AppendLine("popup.document.write('<tr><td>');");
        sb.AppendLine("popup.document.write('This is my string');");
        sb.AppendLine("popup.document.write('</td><td>')");
        sb.AppendLine("popup.document.write('This is my string');");
        sb.AppendLine("popup.document.write('</td></tr></table>');");
        sb.AppendLine("popup.document.write('</body></html>');");

            Page.ClientScript.RegisterStartupScript(this.GetType(), "OpenWindow", sb.ToString(), true);
        }  
        

        Many thanks...

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups