簡體   English   中英

如何在asp.net中進行打印(點陣打印機)

[英]how to give print(dot matrix printer) in asp.net

在我的項目中,我必須打印一張表格,例如姓名,年齡等。所有表格都打印在打印紙上,我只需要根據紙張放置姓名,年齡即可。 目前,我正在使用一個asp.net頁面來實現我的目的。在這里我放置了position = absolute的標簽。 當我單擊打印時,我將調用該頁面,在onload事件中,我將根據前一頁的內容放置標簽值。

很簡單。 那么是否有更好的方法可以在dotmatrix打印機中打印? 請提出建議。

現在打印效果很好,但是問題是當我單擊打印按鈕時,我會像彈出窗口一樣打開該頁面並在其中調用打印。 但我想在單擊打印或取消按鈕后關閉該彈出窗口。 請幫我。

我的代碼是這樣的:

此BtnPrint在我的主頁中。 主頁包含名稱,年齡等輸入。在PrtPage中,我根據給定的打印空間放置了標簽。因此,我在PrtPage.aspx的onload事件中查找主頁值。

受保護的void BtnPrint_Click(對象發送者,EventArgs e)

{

    Response.Write("<script>");
    Response.Write("window.open('PrtPage.aspx','_blank')");
    Response.Write("</script>");
}

在PrtPage的頁面加載中:

受保護的void Page_Load(對象發送者,EventArgs e)

{

     Response.Write("<script>");
     Response.Write("window.print()");
     //Response.Write("window.close()");
     Response.Write("<script>");
}

但是每當我單擊“打印”按鈕時,它都會詢問“您要關閉窗口嗎?”,因此請幫助我。 我想在點擊“打印”或在“打印設置”窗口中取消后關閉。

或建議我,如果在dotmatrix中有任何好的打印方法。

山姆

您得到這些的原因是asp.net的范圍僅限於瀏覽器窗口。 如果將瀏覽器設置為詢問用戶在嘗試自動關閉頁面時是否要關閉該頁面(大多數情況下看起來是這樣),則無法從瀏覽器中阻止該頁面。

與打印相同,您不能自動打印,因為瀏覽器不允許您打印。

關於使用單個頁面而不是打開彈出窗口呢? 在下面的博客文章中,您可以查看示例用法。 隱藏了具有可打印和不可打印內容的兩個div標簽以及具有可打印內容的div標簽。 然后,您可以使用Jquery在DIV中打印內容。

http://itzonesl.blogspot.com/2013/02/how-to-print-content-inside-div-tag.html

更新:

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server" >
<title></title>
<script src="http://code.jquery.com/jquery-1.8.2.min.js"></script>
 <script src="jquery.printElement.js" type="text/javascript"></script>
<script type="text/javascript">
     function printpage() {
        $("#lblName").html($("#TextBox1").val());
        $("#lblSchool").html($("#TextBox2").val());
        $("#printable").printElement();
    }

</script>
<style type="text/css">
#printable { display: none; }
@media print
 {
     #nonprintable { display: none; }
     #printable { display: block; }
 }

</style>
</head>
<body>

    <form id="form1" runat="server">
     <div id="nonprintable">

        <table class="style1">
            <tr>
                <td>
                    <asp:Label ID="Label1" runat="server" Text="Name"></asp:Label>:
                </td>
                <td>
                    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td>
                    <asp:Label ID="Label2" runat="server" Text="School"></asp:Label>:
                </td>
                <td>
                    <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
                </td>
            </tr>
        </table>

 </div>
     <div id="printable">
 <table class="style1">
            <tr>
                <td>
                    <asp:Label ID="Label3" runat="server" Text="Name"></asp:Label>:
                </td>
                <td>
                    <asp:Label ID="lblName" runat="server" ></asp:Label>
                </td>
            </tr>
            <tr>
                <td>
                    <asp:Label ID="Label4" runat="server" Text="School"></asp:Label>:
                </td>
                <td>
                    <asp:Label ID="lblSchool" runat="server" ></asp:Label>
                </td>
            </tr>
        </table>
 </div>
    <asp:Button ID="Button1" runat="server" Text="Print" 
        OnClientClick="printpage();" />
    </form>
</body>
</html>

使用MasterPage:

MasterPage.aspx

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head runat="server">
    <title></title>
    <link href="~/Styles/Site.css" rel="stylesheet" type="text/css" />
    <asp:ContentPlaceHolder ID="HeadContent" runat="server">
    </asp:ContentPlaceHolder>
</head>
<body>
    <form runat="server">
    <div class="page">
        <div class="header">
            <div class="title">
                <h1>
                    My ASP.NET Application
                </h1>
            </div>
              <div class="clear hideSkiplink">
                <asp:Menu ID="NavigationMenu" runat="server" CssClass="menu" EnableViewState="false" IncludeStyleBlock="false" Orientation="Horizontal">
                    <Items>
                        <asp:MenuItem NavigateUrl="~/Default.aspx" Text="Home"/>
                        <asp:MenuItem NavigateUrl="~/About.aspx" Text="About"/>
                    </Items>
                </asp:Menu>
            </div>
        </div>
        <div class="main">
            <asp:ContentPlaceHolder ID="MainContent" runat="server"/>
        </div>
        <div class="clear">
        </div>
    </div>
    <div class="footer">
    </div>
    </form>
</body>
</html>

ContentPage.aspx

<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" Runat="Server">
<script src="http://code.jquery.com/jquery-1.8.2.min.js"></script>
 <script src="jquery.printElement.js" type="text/javascript"></script>
<script type="text/javascript">
     function printpage() {
        $("#MainContent_lblName").html($("#MainContent_TextBox1").val());
        $("#MainContent_lblSchool").html($("#MainContent_TextBox2").val());
        $("#printable").printElement();
    }

</script>
<style type="text/css">
#printable { display: none; }
@media print
 {
     #nonprintable { display: none; }
     #printable { display: block; }
 }

</style>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" Runat="Server">
     <div id="nonprintable">

        <table class="style1">
            <tr>
                <td>
                    <asp:Label ID="Label1" runat="server" Text="Name"></asp:Label>:
                </td>
                <td>
                    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td>
                    <asp:Label ID="Label2" runat="server" Text="School"></asp:Label>:
                </td>
                <td>
                    <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
                </td>
            </tr>
        </table>

 </div>
     <div id="printable">
 <table class="style1">
            <tr>
                <td>
                    <asp:Label ID="Label3" runat="server" Text="Name"></asp:Label>:
                </td>
                <td>
                    <asp:Label ID="lblName" runat="server" ></asp:Label>
                </td>
            </tr>
            <tr>
                <td>
                    <asp:Label ID="Label4" runat="server" Text="School"></asp:Label>:
                </td>
                <td>
                    <asp:Label ID="lblSchool" runat="server" ></asp:Label>
                </td>
            </tr>
        </table>
 </div>
    <asp:Button ID="Button1" runat="server" Text="Print" 
        OnClientClick="printpage();" />
</asp:Content>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM