我知道打印整个GridView并不是一个太大的挑战。但是,我希望打印一个特定的行。不仅仅是它如何出现在gridview中,而是所有行单元格在页面上连接在一起。每行都是一个简历,有一个ID,一个名称列,一个标题列,一个正文列,一个作者列和一个创建日期的列。对于body列,仅显示前50个字符 - 因此只显示在显示的行时将无效。
我希望能够打印行并对其进行格式化,使其看起来像一个实际的简历:
Name Title Date
Body
ResumeID
这可能吗?
我还要为每一行添加一个打印图标,可以触发onclick
或onclientclick
事件。
aspx的一部分:
<div id="GridViewData">
<asp:GridView ID="GVResume" runat="server" AutoGenerateColumns="False" Width="100%"
HeaderStyle-BorderWidth="1" HeaderStyle-BorderStyle="Solid"
OnRowDataBound="GVResume_OnRowDataBound"
OnRowCommand="GVResume_OnRowCommand">
<Columns>
<asp:TemplateField ItemStyle-Wrap="false" ItemStyle-VerticalAlign="Top" ItemStyle-HorizontalAlign="Left" HeaderText="ID" Visible="false">
<ItemTemplate>
<asp:Label ID="lblID" runat="server" Text='<%# Eval("ID") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="DateCreated" HeaderText="Date Created" ItemStyle-HorizontalAlign="Left" ItemStyle-VerticalAlign="Top" ItemStyle-Wrap="false" />
<asp:BoundField DataField="Title" HeaderText="Title" ItemStyle-HorizontalAlign="Left" ItemStyle-VerticalAlign="Top" ItemStyle-Wrap="false" />
<asp:BoundField DataField="Author" HeaderText="Author" ItemStyle-HorizontalAlign="Left" ItemStyle-VerticalAlign="Top" ItemStyle-Wrap="false" />
<asp:TemplateField ItemStyle-Wrap="false" ItemStyle-VerticalAlign="Top" ItemStyle-HorizontalAlign="Left" HeaderText="Body">
<ItemTemplate>
<asp:Label ID="lblBody" runat="server" Text='<%# Shorten(Convert.ToString(Eval("Body"))) %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField ItemStyle-Wrap="false" ItemStyle-VerticalAlign="Top">
<ItemTemplate>
<asp:ImageButton ID="IBPrint" runat="server" Onclick="IBPrint_Click1" ImageUrl="~/images/print.png" Height="20" Width="20" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
aspx.cs的一部分:
public override void VerifyRenderingInServerForm(Control control)
{
//base.VerifyRenderingInServerForm(control);
}
protected string Shorten(string Body)
{
if (Body.Length > 50)
{
Body= Body.Substring(0, 50) + "...";
}
return Body;
}
private void BindIt()
{
GVResume.DataSource = Data.GetInfo(UserID);
GVResume.DataBind();
}
//need to print row and format, not entire gridview
protected void IBPrint_Click1(object sender, ImageClickEventArgs e)
{
GVResume.DataBind();
StringWriter Writer = new StringWriter();
HtmlTextWriter HtmlWriter = new HtmlTextWriter(Writer);
GVResume.RenderControl(HtmlWriter);
string GrdHtml = Writer.ToString().Replace("\"", "'").Replace(System.Environment.NewLine, "");
StringBuilder BuildAJsString = new StringBuilder();
BuildAJsString.Append("<script type = 'text/javascript'>");
BuildAJsString.Append("window.onload = new function(){");
BuildAJsString.Append("var print = window.open('', '', 'left=0");
BuildAJsString.Append(",top=0,width=1000,height=600,status=0');");
BuildAJsString.Append("print.document.write(document.getElementById('GridViewData').innerHTML);");
BuildAJsString.Append("print.document.close();");
BuildAJsString.Append("print.focus();");
BuildAJsString.Append("print.print();};");
BuildAJsString.Append("</script>");
ClientScript.RegisterStartupScript(this.GetType(), "GridPrint", BuildAJsString.ToString());
BindIt();
}
答案 0 :(得分:1)
自己想出来,基本上只是将行注入gridview下面的隐藏div,然后打印出它的内部html。我确定还有其他方法,但效果很好:
ASPX:
<asp:Button ID="btnToggleViews" runat="server" Text="Show Bounces" OnClick="btnToggleViews_Click" />
<asp:Label ID="lblParentID" runat="server" Visible="false"></asp:Label><asp:ImageButton ID="IBPrintGridview" runat="server" OnClick="IBPrintGridview_Click1" ImageUrl="~/images/printer.gif" Height="26" Width="26" />
<asp:Label ID="Label1" CssClass="Label1" runat="server" Text="0" Visible="false" Font-Bold="true" BorderColor="#507CD1" BorderWidth="2.5" ForeColor="White" BackColor="#507CD1"></asp:Label>
<div id="GridViewData">
<asp:GridView ID="GVResume" runat="server" AutoGenerateColumns="False" Width="100%"
HeaderStyle-BorderWidth="1" HeaderStyle-BorderStyle="Solid"
OnRowDataBound="GVResume_OnRowDataBound"
OnRowCommand="GVResume_OnRowCommand">
<Columns>
<asp:TemplateField ItemStyle-Wrap="false" ItemStyle-VerticalAlign="Top" ItemStyle-HorizontalAlign="Left" HeaderText="ID" Visible="false">
<ItemTemplate>
<asp:Label ID="lblID" runat="server" Text='<%# Eval("ID") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="DateCreated" HeaderText="Date Created" ItemStyle-HorizontalAlign="Left" ItemStyle-VerticalAlign="Top" ItemStyle-Wrap="false" />
<asp:BoundField DataField="Title" HeaderText="Title" ItemStyle-HorizontalAlign="Left" ItemStyle-VerticalAlign="Top" ItemStyle-Wrap="false" />
<asp:BoundField DataField="Author" HeaderText="Author" ItemStyle-HorizontalAlign="Left" ItemStyle-VerticalAlign="Top" ItemStyle-Wrap="false" />
<asp:TemplateField ItemStyle-Wrap="false" ItemStyle-VerticalAlign="Top" ItemStyle-HorizontalAlign="Left" HeaderText="Body">
<ItemTemplate>
<asp:Label ID="lblBody" runat="server" Text='<%# Shorten(Convert.ToString(Eval("Body"))) %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField ItemStyle-Wrap="false" ItemStyle-VerticalAlign="Top">
<ItemTemplate>
<asp:ImageButton ID="IBPrint" runat="server" Onclick="IBPrint_Click1" ImageUrl="~/images/print.png" Height="20" Width="20"
CommandArgument='<%# "ID: " + Eval("ID") + " - " + Eval("DateCreated") + "<br /><br /><b>Author:</b> " + Eval("Author") + "<br /><b>Title:</b> " + Eval("Title") + "<br /><br /><b>Body:</b><br /><br /> " + Eval("Body") %>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
<div id="EmailPrint" style="visibility: hidden">
<div style="text-align:right"><asp:ImageButton ID="PrintEmail" runat="server" OnClientClick="window.print()" ImageUrl="~/images/print.png" Height="20" Width="20" /></div>
<hr />
<asp:Label ID="Label3" runat="server"></asp:Label>
</div>
aspx.cs:
protected void GVResume_RowCommand(object sender, GridViewCommandEventArgs e)
{
if(e.CommandName == "Print")
{
Label3.Text = Argument;
PrintIt();
}
}
private void PrintIt()
{
GVResume.DataBind();
StringBuilder BuildAJsString = new StringBuilder();
BuildAJsString.Append("<script type = 'text/javascript'>");
BuildAJsString.Append("window.onload = new function(){");
BuildAJsString.Append("var print = window.open('', '', 'left=0");
BuildAJsString.Append(",top=0,width=1000,height=600,status=0');");
BuildAJsString.Append("print.document.write(document.getElementById('EmailPrint').innerHTML);");
BuildAJsString.Append("print.document.close();");
BuildAJsString.Append("print.focus();");
BuildAJsString.Append("};");
BuildAJsString.Append("</script>");
ClientScript.RegisterStartupScript(this.GetType(), "GridPrint", BuildAJsString.ToString());
BounceCount();
}
protected void IBPrint_Click1(object sender, ImageClickEventArgs e)
{
GVResume.DataBind();
StringBuilder BuildAJsString = new StringBuilder();
BuildAJsString.Append("<script type = 'text/javascript'>");
BuildAJsString.Append("window.onload = new function(){");
BuildAJsString.Append("var print = window.open('', '', 'left=0");
BuildAJsString.Append(",top=0,width=1000,height=600,status=0');");
BuildAJsString.Append("print.document.write(document.getElementById('GridViewData').innerHTML);");
BuildAJsString.Append("print.document.close();");
BuildAJsString.Append("print.focus();");
BuildAJsString.Append("print.print();};");
BuildAJsString.Append("</script>");
ClientScript.RegisterStartupScript(this.GetType(), "GridPrint", BuildAJsString.ToString());
BounceCount();
}