Reputation: 3132
I have a page in ASP.NET. In this page, I have a List<string>
which contains various data. I need to be able to print this information on the client side. So if a logged on user clicks the print button, he is shown a dialog from which to choose settings like which printer, landscape/portrait, number if copies, etc. This is my code:
private List<string> dataToBePrinted = new List<string>();
public void addDataToList(string text)
{
dataToBePrinted.Add(text);
}
protected void btnPrint_Click(object sender, EventArgs e)
{
//Print contents of dataToBePrinted to local printer here.
}
So far, I have only been able to print the page as-is (with all controls, windows, etc.) but I only need the info in the List<string>
. How can I accomplish this?
EDIT:
Ok thanks for all the comments/answers guys. I got it working:
//When clicking "Print" data, put list in session variables and redirect to new blank page
protected void btnGruppenkalenderDrucken_Click(object sender, EventArgs e)
{
Session["Printing"] = lstData;
Response.Redirect("Printing.aspx");
}
//This is the code behind of the new blank page. Cast session variable back to list, then fill table
public partial class Printing : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
List<List<string>> lst = (List<List<string>>)Session["Printing"];
foreach (List<string> temp in lst)
{
TableRow row = new TableRow();
foreach (string data in temp)
{
TableCell cell = new TableCell();
cell.Text = data;
row.Cells.Add(cell);
}
tblData.Rows.Add(row);
}
}
}
}
//Markup of new page. I added button "btnPrint" that calls javascript for printing when clicked client side.
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Printing.aspx.cs" Inherits="WerIstWo.Printing" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Table ID="tblData" runat="server">
</asp:Table>
<asp:Button ID="btnPrint" runat="server" Text="Print" OnClientClick="javascript:window.print();"/>
</div>
</form>
</body>
</html>
Now I will just have to format the blank page and all is well.
Thanks again!
Upvotes: 0
Views: 5583
Reputation: 22074
Take a look at css media selector. Define css class like this:
.printable {
display:none;
}
@media print {
.noPrint {
display:none;
}
.printable {
display:block;
}
}
Then add this class to all elements on your page, except the element in which you'll dump the List data. this element should have the printable class.
Reference:
Use CSS to hide contents on print
Upvotes: 0
Reputation: 5480
My suggestion would be to create a new page that lays the data out how you want it, redirect the user there and then in javascript call windows.print()
.
Upvotes: 1
Reputation: 1622
Because of how web browsers are designed the server (your code) will not have direct access to the client's printer. Instead, you can have a link/button to a 'print page' or a page that only has the information you need (the contents of that list). The client can then print that page selecting their own local printer.
You could include some javascript in the print page after the main body that will prompt the user to print.
window.print()
Should do what you want.
Upvotes: 1