c# - 如何使用 ASP.net 打印标签中的数据?

标签 c# javascript asp.net printing

在我的 asp.net 网站中,我需要打印标签内的信息。标签内的信息将从数据库中获取。 标签将包含客户姓名、地址、电话号码等数据。 我需要将其打印在一张纸上。

在我的网页上,我有一个按钮和一个 HTML 表格。 HTML 表包含客户信息。一切对我来说都很好。我可以拿到打印件。但我想将打印页面上的数据格式化。前任。中心对齐,具有一些字体和字体大小。我做不到。

以下是我的打印预览页enter image description here

这是我的打印页面 enter image description here

这很好。但我想让带有任何 Logo 的打印页面更加时尚。文本的大小需要增加,表格应该居中。如果我能在打印页面中为表格添加边框,那就太好了。基本上我想在打印页面中做一些样式。

我尝试实现 printstyles css 但我无法得到任何结果。

我在“打印”按钮的按钮单击事件期间使用的代码是

protected void btnPrint_Click(object sender, EventArgs e)
{

    StringWriter sw = new StringWriter();
    HtmlTextWriter hw = new HtmlTextWriter(sw);
    StringBuilder sb = new StringBuilder();
    sb.Append("<script type = 'text/javascript'>");
    sb.Append("window.onload = new function(){");
    sb.Append("var divToPrint=document.getElementById('print');");
    sb.Append("var printWin = window.open('', '', 'left=0");
    sb.Append(",top=0,width=800,height=600,status=0');");
    //sb.Append("printWin.document.write(<link rel='stylesheet' type='text/css' href='PrintStyle.css' media='print'/>\n);");
    sb.Append("printWin.document.write(divToPrint.outerHTML);");
    sb.Append("printWin.document.close();");
    sb.Append("printWin.focus();");
    sb.Append("printWin.print();}");
    sb.Append("</script>");
    ClientScript.RegisterStartupScript(this.GetType(), "GridPrint", sb.ToString());

}

如果有人能帮助我做到这一点,那就太好了。

最佳答案

尝试这个,但将标签放在 div 内,并为 div 提供 id,如“mydiv”,就像我在下面使用的那样。

<html>
<head>
<script type="text/javascript" src="http://jqueryjs.googlecode.com/files/jquery-1.3.1.min.js" > </script> 
<script type="text/javascript">

function PrintElem(elem)
{
    Popup($(elem).html());
}

function Popup(data) 
{
    var mywindow = window.open('', 'my div', 'height=400,width=600');
    mywindow.document.write('<html><head><title>my div</title>');
    /*optional stylesheet*/ //mywindow.document.write('<link rel="stylesheet" href="main.css" type="text/css" />');
    mywindow.document.write('</head><body >');
    mywindow.document.write(data);
    mywindow.document.write('</body></html>');

    mywindow.print();
    mywindow.close();

    return true;
}

</script>
</head>

<div id="mydiv">
This will be printed. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque a quam at nibh adipiscing interdum. Nulla vitae accumsan ante. 
</div>  
 <input type="button" value="Print Div" onclick="PrintElem('#mydiv')" />
 </body>
 </html>

关于c# - 如何使用 ASP.net 打印标签中的数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14051221/

相关文章:

javascript - Nodejs 自定义错误类抛出 "TypeError: ValidationError is not a constructor"

javascript - 我如何使用reactjs访问json中的对象数组中的数据

javascript - 修改 Canvas 对象

c# - ASP.Net 应用程序对回发的加倍控制

asp.net - Server.UrlEncode 与 HttpUtility.UrlEncode

c# - 单击下一步转到第一个选项卡时,ASP.NET C# datagridview 在第二个选项卡内分页

c# - c#中的锁定机制

c# - C#中线程的合理使用?

c# - .NET/VB6 应用程序在 Windows 7 上失败

c# - 为什么我收到 "Expression always true"警告?