我在 ASP.NET 中有一个页面。在这个页面中,我有一个List<string>
包含各种数据的。我需要能够在客户端打印此信息。因此,如果登录的用户单击打印按钮,则会显示一个对话框,从中可以选择打印机、横向/纵向、副本数量等设置。这是我的代码:
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.
}
到目前为止,我只能按原样打印页面(包含所有控件、窗口等),但我只需要List<string>
. 我怎样才能做到这一点?
编辑:
好的,感谢所有评论/答案的家伙。我让它工作:
//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>
现在我只需要格式化空白页,一切都很好。
再次感谢!