打印字符串变量的内容对本地打印机ASP.NET [英] Printing contents of string-variable to local printer ASP.NET

查看:128
本文介绍了打印字符串变量的内容对本地打印机ASP.NET的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在ASP.NET的页面。在这个页面中,我有一个列表与LT;串> ,其中包含各种数据。我需要能够打印在客户端信息。因此,如果登录的用户点击打印按钮时,他表示从该对话框中选择设置,如哪台打印机,横向/纵向,号码,如果副本等等,这是我的code:

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.
 }

到目前为止,我只能够打印页面,是(与所有控件,窗等),但我只需要在列表与LT的信息;串&GT; 。我怎样才能做到这一点?

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?

编辑:

所有的意见/回答你们好,谢谢。我得到它的工作:

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.

再次感谢!

推荐答案

不会有客户端的打印机上直接访问。相反,你可以有一个链接/按钮,一个打印此页,或者只有你所需要的信息(即列表中的内容)的页面。然后,客户机可以打印页面选择自己的本地打印机。

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.

您可以包括在主体后打印页一些JavaScript,将提示用户进行打印。

You could include some javascript in the print page after the main body that will prompt the user to print.

window.print()如果你想要做什么。

这篇关于打印字符串变量的内容对本地打印机ASP.NET的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆