Print to client PC's printer
-
I have an ASP.Net web application using C# and I would like to know how do I print what is currently displayed on the web page to the client PC's printer by clicking a button on the web page, thanks.
You can use the javascript function
window.print();
to initiate a client-side print dialog. So you could have a normal HTML button like this:<input type="button" onclick="window.print();"
value="Print"
/>or an ASP.NET server control button like this:
<asp:Button id="btnPrint" runat="server"
Text="Print"
OnClientClick="window.print();"
/>The advantage (or disadvantage?) of the latter is that it will still post back to the server. This could be useful if you wanted to log the fact that the user used the Print functionality from the button. You could eliminate the postback in the server control by adding
return false;
to the OnClientClick code:<asp:Button id="btnPrint" runat="server"
Text="Print"
OnClientClick="window.print(); return false;"
/> -
You can use the javascript function
window.print();
to initiate a client-side print dialog. So you could have a normal HTML button like this:<input type="button" onclick="window.print();"
value="Print"
/>or an ASP.NET server control button like this:
<asp:Button id="btnPrint" runat="server"
Text="Print"
OnClientClick="window.print();"
/>The advantage (or disadvantage?) of the latter is that it will still post back to the server. This could be useful if you wanted to log the fact that the user used the Print functionality from the button. You could eliminate the postback in the server control by adding
return false;
to the OnClientClick code:<asp:Button id="btnPrint" runat="server"
Text="Print"
OnClientClick="window.print(); return false;"
/> -
This is cool. Although I suppose you would actually want to navigate to a printer-friendly page before you print, then on page.load you would call window.print(); ? Jason
Sounds good to me :)
-
Sounds good to me :)
I implemented your suggestion, but it doesn't print out everything that I want. What I have on the page are checkboxes, a dropdownlist box that allows me to select an hourly interval, a button that said "Generate Report" and another button that said "Print". I also have a DataGrid that is initially invisible. After selecting the checkboxes and pressing the "Generate Report" button, the DataGrid becomes visible displaying a report in tabular form. When I click the print button after pressing the generate report button, the printout only shows the checkboxes with nothing selected, the dropdownlist box with the default selection, and the two buttons. The DataGrid is not printed. How do I get it to print what is currently displayed on the page instead of what was displayed before I clicked the "Generate Report" button, thanks.
-
I implemented your suggestion, but it doesn't print out everything that I want. What I have on the page are checkboxes, a dropdownlist box that allows me to select an hourly interval, a button that said "Generate Report" and another button that said "Print". I also have a DataGrid that is initially invisible. After selecting the checkboxes and pressing the "Generate Report" button, the DataGrid becomes visible displaying a report in tabular form. When I click the print button after pressing the generate report button, the printout only shows the checkboxes with nothing selected, the dropdownlist box with the default selection, and the two buttons. The DataGrid is not printed. How do I get it to print what is currently displayed on the page instead of what was displayed before I clicked the "Generate Report" button, thanks.
Well, the javascript
window.print()
should print out whatever the browser has rendered. I'm guessing your "Print" button is first posting back to the server? -
Well, the javascript
window.print()
should print out whatever the browser has rendered. I'm guessing your "Print" button is first posting back to the server?For my webpage, I am using the VS IDE to design the page and putting the C# code in the codebehind. When I click on the html tab in the designer to put in OnClientClick="window.print(); return false;" for my print button, the designer draws a red line underneath OnClientClick and the print button doesn't work. When I added this code in the codebehind, private void PrintBtn_Click(object sender, System.EventArgs e) { string strJScript2 = "window.print();"; Page.RegisterStartupScript("Alert", strJScript2); } then the print button works correctly when I am viewing the page from the server. When I am viewing the page from a client PC it would print whatever is on the page before I press the "Generate Report" button.
-
For my webpage, I am using the VS IDE to design the page and putting the C# code in the codebehind. When I click on the html tab in the designer to put in OnClientClick="window.print(); return false;" for my print button, the designer draws a red line underneath OnClientClick and the print button doesn't work. When I added this code in the codebehind, private void PrintBtn_Click(object sender, System.EventArgs e) { string strJScript2 = "window.print();"; Page.RegisterStartupScript("Alert", strJScript2); } then the print button works correctly when I am viewing the page from the server. When I am viewing the page from a client PC it would print whatever is on the page before I press the "Generate Report" button.
Are you using ASP.NET 2.0?
-
Are you using ASP.NET 2.0?
-
That's why
OnClientClick
didn't work - that's a 2.0 attribute. Okay, then - try using a straight html/client way of hanlding this:<input type="button" onclick="window.print();"
value="Print"
/> -
That's why
OnClientClick
didn't work - that's a 2.0 attribute. Okay, then - try using a straight html/client way of hanlding this:<input type="button" onclick="window.print();"
value="Print"
/> -
I have a slight problem with the window.print() function. When there are more data than what my DataGrid can display within its define height, a vertical scrollbar appears. When the user clicks the print button, it only prints out what is currently display on the screen. Is there a way for me to get it to print all the data that is on the DataGrid instead of having the user scroll down on the DataGrid to print out the rest of the data? I have noticed that certain websites have a "Printer Friendly Version" button that when clicked will format all the information on the page so that all the contents can be printed. Is there a way to do this in C#, thanks.