kid.live says:
gridHTML +=
string
.Format(
"<img id='SpeIma' src='{0}' style='width: 200px; height: 68px; margin-bottom:25px;' />"
, ResolveUrl(imgurl));
Problem in the img tag single quote. Replace with double quote and add escape character.
Please refer below sample.
Note: For this sample i have used temporary DataTable. For more details refer How to create Temporary Table in ASP.Net using C# and VB.Net.
HTML
<asp:GridView ID="gv_Details" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="Name" HeaderText="Name" ItemStyle-Width="150" />
<asp:BoundField DataField="Country" HeaderText="Country" ItemStyle-Width="150" />
</Columns>
</asp:GridView>
<br />
<asp:Button ID="btnPrintAll" CssClass="btn btn-dark btn-block mb-2" runat="server" Text="طباعة الكل صفحة" OnClick="PrintGridView" CommandArgument="All" />
<script type="text/javascript">
function PrintGrid(html, css) {
var printWin = window.open('', '', 'left=0,top=0,width=1002,height=750,scrollbars=1');
printWin.document.write('<style type = "text/css">' + css + '</style>');
printWin.document.write(html);
printWin.document.close();
printWin.focus();
printWin.print();
printWin.close();
};
</script>
Namespaces
C#
using System.Data;
using System.IO;
VB.Net
Imports System.Data
Imports System.IO
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
GetDataFromJSON();
}
}
protected void GetDataFromJSON()
{
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[3] { new DataColumn("Id"), new DataColumn("Name"), new DataColumn("Country") });
dt.Rows.Add(1, "John Hammond", "United States");
dt.Rows.Add(2, "Mudassar Khan", "India");
dt.Rows.Add(3, "Suzanne Mathews", "France");
dt.Rows.Add(4, "Robert Schidner", "Russia");
gv_Details.DataSource = dt;
gv_Details.DataBind();
}
protected void PrintGridView(object sender, EventArgs e)
{
//Disable Paging if all Pages need to be Printed.
if ((sender as Button).CommandArgument == "All")
{
//Disable Paging.
gv_Details.AllowPaging = false;
//Re-bind the GridView.
GetDataFromJSON();
//For Printing Header on each Page.
gv_Details.UseAccessibleHeader = true;
gv_Details.HeaderRow.TableSection = TableRowSection.TableHeader;
gv_Details.FooterRow.TableSection = TableRowSection.TableFooter;
gv_Details.Attributes["style"] = "border-collapse:separate";
foreach (GridViewRow row in gv_Details.Rows)
{
if ((row.RowIndex + 1) % gv_Details.PageSize == 0 && row.RowIndex != 0)
{
row.Attributes["style"] = "page-break-after:always;";
}
}
}
else
{
//Hide the Pager.
gv_Details.PagerSettings.Visible = false;
GetDataFromJSON();
}
using (StringWriter sw = new StringWriter())
{
//Render GridView to HTML.
HtmlTextWriter hw = new HtmlTextWriter(sw);
gv_Details.RenderControl(hw);
//Enable Paging.
gv_Details.AllowPaging = true;
this.GetDataFromJSON();
//Remove single quotes to avoid JavaScript error.
string gridHTML = sw.ToString().Replace(Environment.NewLine, "");
string imgurl = "assets/img/5m-transparent-logo.png";
gridHTML += string.Format("<img id=\"SpeIma\" src=\"{0}\" style=\"width: 200px; height: 68px; margin-bottom:25px;\" />", ResolveUrl(imgurl));
string script = "window.onload = function() { PrintGrid('" + gridHTML + "', ''); }";
ClientScript.RegisterStartupScript(this.GetType(), "GridPrint", script, true);
}
}
public override void VerifyRenderingInServerForm(Control control)
{
/* Verifies that the control is rendered */
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
GetDataFromJSON()
End If
End Sub
Protected Sub GetDataFromJSON()
Dim dt As DataTable = New DataTable()
dt.Columns.AddRange(New DataColumn(2) {New DataColumn("Id"), New DataColumn("Name"), New DataColumn("Country")})
dt.Rows.Add(1, "John Hammond", "United States")
dt.Rows.Add(2, "Mudassar Khan", "India")
dt.Rows.Add(3, "Suzanne Mathews", "France")
dt.Rows.Add(4, "Robert Schidner", "Russia")
gv_Details.DataSource = dt
gv_Details.DataBind()
End Sub
Protected Sub PrintGridView(ByVal sender As Object, ByVal e As EventArgs)
If (TryCast(sender, Button)).CommandArgument = "All" Then
gv_Details.AllowPaging = False
GetDataFromJSON()
gv_Details.UseAccessibleHeader = True
gv_Details.HeaderRow.TableSection = TableRowSection.TableHeader
gv_Details.FooterRow.TableSection = TableRowSection.TableFooter
gv_Details.Attributes("style") = "border-collapse:separate"
For Each row As GridViewRow In gv_Details.Rows
If (row.RowIndex + 1) Mod gv_Details.PageSize = 0 AndAlso row.RowIndex <> 0 Then
row.Attributes("style") = "page-break-after:always;"
End If
Next
Else
gv_Details.PagerSettings.Visible = False
GetDataFromJSON()
End If
Using sw As StringWriter = New StringWriter()
Dim hw As HtmlTextWriter = New HtmlTextWriter(sw)
gv_Details.RenderControl(hw)
gv_Details.AllowPaging = True
Me.GetDataFromJSON()
Dim gridHTML As String = sw.ToString().Replace(Environment.NewLine, "")
Dim imgurl As String = "assets/img/5m-transparent-logo.png"
gridHTML += String.Format("<img id=""SpeIma"" src=""{0}"" style=""width: 200px; height: 68px; margin-bottom:25px;"" />", ResolveUrl(imgurl))
Dim script As String = "window.onload = function() { PrintGrid('" & gridHTML & "', ''); }"
ClientScript.RegisterStartupScript(Me.GetType(), "GridPrint", script, True)
End Using
End Sub
Public Overrides Sub VerifyRenderingInServerForm(ByVal control As Control)
End Sub
Screenshot