170635e49469dbb4197a0bcddafc866d

        public static void Print(string ElementID, string SecondElementID)
        {
            Page page = HttpContext.Current.CurrentHandler as Page;

            StringBuilder sb = new StringBuilder();
            sb.Append("<script type = 'text/javascript'>");
            sb.Append("var divID_1 = document.getElementById('" + ElementID + "');");
            sb.Append("var divID_2 = document.getElementById('" + SecondElementID + "');");
            sb.Append("divID_1.border = 0;");
            sb.Append("divID_2.border = 0;");
            sb.Append("var prtwin = window.open('', 'Report', 'left=150, top=150, width=1000, height=1000, tollbar=0, scrollbars=1, status=0, resizable=1');");
            sb.Append("prtwin.document.write(divID_1.outerHTML);");
            sb.Append("prtwin.document.write(divID_2.outerHTML);");
            sb.Append("prtwin.document.close();");
            sb.Append("prtwin.focus();");
            sb.Append("prtwin.print();");
            sb.Append("prtwin.close();");
            sb.Append("</script>");
            page.ClientScript.RegisterStartupScript(page.GetType(), "GridPrint", sb.ToString());
        }

How can i customize this print preview into Something like this
7f0a04405261062bb4247d3124b6399d

Recommended Answers

All 8 Replies

The red words above is written down <Mini Header>

They look the same except for the <Mini Header>. Am i missing something here?

Nope, you take a look of both pictures, the line <hr/> is missing already one.

So for instance, if you want to add an <hr/> element between div1 and div2, then you can do it this way...

sb.Append("var divID_1 = document.getElementById('div1');");
sb.Append("divID_1.insertAdjacentHTML('beforeend', '<hr />');");
sb.Append("var divID_2 = document.getElementById('div2');");
<asp:Content ID="Content1" ContentPlaceHolderID="CPHInformation" runat="server">
    <div id="Report_Word">
        <div id="SubTitle">
            <hr style="margin-right: 30px" />
            <h1>Monthly Extra Income</h1>
            <hr style="margin-right: 30px" />
        </div>
        <br />    

But now i have those <hr/> elements enclosed with div id="SubTitle"
Is possible to remove the <hr/> ?

Now i am trying to pass a string value to the print method Date
But how can i assign it to the javascript?

        public static void Print(string Header, string DataInsertID, string Date)
        {
            Page page = HttpContext.Current.CurrentHandler as Page;

            StringBuilder sb = new StringBuilder();
            sb.Append("<script type = 'text/javascript'>");
            sb.Append("var Header = document.getElementById('" + Header + "');");
            sb.Append("var DataInsertID = document.getElementById('" + DataInsertID + "');");
            sb.Append("var Date = '<%=" + Date + "%>.value';");
            sb.Append("var prtwin = window.open();");
            sb.Append("prtwin.document.write('MSJ Bank Financial - ');");
            sb.Append("prtwin.document.write(Header.outerHTML);");
            sb.Append("prtwin.document.write(Date);");
            sb.Append("prtwin.document.write('<hr />');");            
            sb.Append("prtwin.document.write(DataInsertID.outerHTML);");
            sb.Append("prtwin.document.close();");
            sb.Append("prtwin.focus();");
            sb.Append("prtwin.print();");
            sb.Append("prtwin.close();");
            sb.Append("</script>");
            page.ClientScript.RegisterStartupScript(page.GetType(), "Report", sb.ToString());
        }
        public static void Print(string Header, string DataInsertID, string Date)
        {
            Page page = HttpContext.Current.CurrentHandler as Page;

            StringBuilder sb = new StringBuilder();
            sb.Append("<script type = 'text/javascript'>");
            sb.Append("var Header = document.getElementById('" + Header + "');");
            sb.Append("var DataInsertID = document.getElementById('" + DataInsertID + "');");
            sb.Append("var Date = '" + Date + "';");
            sb.Append("var prtwin = window.open();");
            sb.Append("prtwin.document.write('MSJ Bank Financial - ');");
            sb.Append("prtwin.document.write(Header.outerHTML);");
            sb.Append("prtwin.document.write(' (');");
            sb.Append("prtwin.document.write(Date);");
            sb.Append("prtwin.document.write(')');");
            sb.Append("prtwin.document.write('<hr />');");            
            sb.Append("prtwin.document.write(DataInsertID.outerHTML);");
            sb.Append("prtwin.document.close();");
            sb.Append("prtwin.focus();");
            sb.Append("prtwin.print();");
            sb.Append("prtwin.close();");
            sb.Append("</script>");
            page.ClientScript.RegisterStartupScript(page.GetType(), "Report", sb.ToString());
        }

Now this code is work fine as i expected.
But now i faced another problem which is once the DataInsertID is not repeater but is Chart Control, then how to print them as well?

public static void PrintWebControl(Control ctrl, string Header, string Date)
        {
            Label lblHeader = new Label();
            lblHeader.Text = "MSJ Bank Financial - " + Header + " (" + Date + ")<br/><hr/><br/>";

            StringWriter stringWrite = new StringWriter();
            System.Web.UI.HtmlTextWriter htmlWrite = new System.Web.UI.HtmlTextWriter(stringWrite);
            if (ctrl is WebControl)
            {
                Unit w = new Unit(100, UnitType.Percentage);
                ((WebControl)ctrl).Width = w;
            }

            Page pg = new Page();
            pg.EnableEventValidation = false;

            HtmlForm frm = new HtmlForm();
            pg.Controls.Add(frm);
            frm.Attributes.Add("runat", "server");
            frm.Controls.Add(lblHeader);
            frm.Controls.Add(ctrl);
            pg.DesignerInitialize();
            pg.RenderControl(htmlWrite);

            string strHTML = stringWrite.ToString();
            HttpContext.Current.Response.Clear();
            HttpContext.Current.Response.Write(strHTML);
            HttpContext.Current.Response.Write("<script>window.print();</script>");
            HttpContext.Current.Response.End();
        }

Now i modify the code to be like this, the chart is able to print it out.
But the problem is my current existing page design is changed, how to solve it problem?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.