Answered by:
Print selected document in gridview

Question
-
User-640323567 posted
Hi,
Can somebody please point me in the right direction to achieve this?
I have a gridview with documents in each row. I am binding the gridview with documents from database. I would like to add a print button in each row or if any other better way to select the document and be able to print.
Thanks in advance for any input.
Friday, June 24, 2016 1:00 PM
Answers
-
User1559292362 posted
Hi ASPbun,
I have a gridview with documents in each row. I am binding the gridview with documents from database. I would like to add a print button in each row or if any other better way to select the document and be able to print.The following link provide a solution by Inject rowing into a hidden div below the gridview. then print the inner html.
http://stackoverflow.com/questions/24151110/printing-certain-row-in-gridview
ASPX Code
<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>
Code Behind
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(); }
Best regards,
Cole Wu
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Monday, June 27, 2016 9:04 AM