Pop up print window on button click
-
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.
-
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.
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 )
-
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 )
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...