如何在asp.net的数据列表中打印每个项目

时间:2019-01-14 08:11:12

标签: asp.net

我正在创建功能打印标签列表产品。 我将数据加载到数据列表 当我单击“打印”按钮时,我想打印每个项目的所有数据 例如:单击打印按钮后,我有十个产品,然后有十个标签打印。 谢谢。 我的代码

html

> <asp:DataList ID="dlContacts" runat="server" RepeatLayout="Table"
> RepeatColumns="3"
>                 CellPadding="2" CellSpacing="2">
>                 <ItemTemplate>
>                     <table cellpadding="2" cellspacing="0" border="1" style="width: 200px; height: 100px; border: dashed 2px #04AFEF;
> background-color: #B0E2F5">
>                         <tr>
> 
>                             <b><u><%# Eval("material") %></u></b></td>
>                         </tr>
>                         <tr>
>                             <td>
>                                 <b>City: </b><%# Eval("material") %><br />
>                                 <b>Postal Code: </b><%# Eval("material") %><br />
>                                 <b>Country: </b><%# Eval("material")%><br />
>                                 <b>Phone: </b><%# Eval("material")%><br />
>                                 <b>Fax: </b><%# Eval("material")%><br />
>                             </td>
>                         </tr>
>                     </table>
>                 </ItemTemplate>
>           
  </asp:DataList>   <asp:Button ID="btIn" runat="server" OnClick="btIn_Click"  Text="Print" />

c#。

 protected void Print(object sender, EventArgs e)
    {
        DataTable dt = BUS.BUS_ItemTemBarcode.GetItem();
        dlproducts.DataSource = dt;
        dlproducts.DataBind();
        List<DataRow> rows = new List<DataRow>();
        StringWriter sw = new StringWriter();
        HtmlTextWriter hw = new HtmlTextWriter(sw);
        dlContacts.RenderControl(hw);
        string html = sw.ToString().Replace("\"", "'").Replace(System.Environment.NewLine, "");
        StringBuilder sb = new StringBuilder();
        sb.Append("<script type = 'text/javascript'>");
        sb.Append("window.onload = new function(){");
        sb.Append("var printWin = window.open('', '', 'left=0");
        sb.Append(",top=0,width=500,height=20,status=0');");
        sb.Append("printWin.document.write(\"");
        sb.Append(html);
        sb.Append("\");"); 
        sb.Append("printWin.document.close();");
        sb.Append("printWin.focus();");
sb.Append("printWin.print();printWin.opener.location.href=printWin.opener.location.href;");
        sb.Append("printWin.close();};");
        sb.Append("</script>");
        ClientScript.RegisterStartupScript(this.GetType(), "GridPrint", sb.ToString());
    }

0 个答案:

没有答案