locked
Repater control Print Problem RRS feed

  • Question

  • User2033107836 posted

    Hello

    My Repater Control look like this with perfect all side border

    This is my print control code on button click

    public static void PrintWebControl(Control ctrl, string Script)
        {
            StringWriter stringWrite = new StringWriter();
            HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
    
            if (ctrl is WebControl)
            {
                Unit w = new Unit(50, UnitType.Pixel);
                ((WebControl)ctrl).Width = w;
            }
    
            Page pg = new Page();
            if (Script != string.Empty)
            {
                pg.RegisterStartupScript("PrintJavaScript", Script);
            }
    
            HtmlForm frm = new HtmlForm();
            pg.Controls.Add(frm);
            frm.Attributes.Add("runat", "server");
            frm.Controls.Add(ctrl);
            string scr = "<script>function window.onafterprint(){history.back(1);}</script>";
            htmlWrite.Write(scr);
            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();
    
        }

    when i click button its automatically removing border alignment and printing why

    Tuesday, July 18, 2017 8:53 AM

Answers

  • User-707554951 posted

    Hi asp.ambur,

    Based on your description, you printed repeater without border alignment.

    Then you could refer to the following code:

    <script type = "text/javascript">
            function PrintPanel() {
                var panel = document.getElementById("<%=Panel1.ClientID %>");
                var printWindow = window.open('', '', 'height=400,width=800');
                printWindow.document.write('<html><head><title>DIV Contents</title>');
                printWindow.document.write('</head><body >');
                printWindow.document.write(panel.innerHTML);
                printWindow.document.write('</body></html>');
                printWindow.document.close();
                setTimeout(function () {
                    printWindow.print();
                }, 500);
                return false;
            }
        </script>
    
    <asp:Panel ID="Panel1" runat="server">
        <div >
            <table id="tbl" border="1" width="200px" style="border-collapse:collapse;">
                <asp:Repeater runat="server" ID="Repeater1">
                   <ItemTemplate>
                       <tr><td><%#Eval("Id")%></td>
                           <td><%#Eval("userName")%></td>
                           <td><%#Eval("userDetail")%></td></td>
                       </tr>
                   </ItemTemplate>
                    </asp:Repeater>
            </table>
        </div>
                </asp:Panel>
            <asp:Button ID="Button1" runat="server" Text="print repeater" OnClientClick = "return PrintPanel();" />

    Output:

    Best regards,

    Cathy

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, July 19, 2017 7:28 AM

All replies

  • User-707554951 posted

    Hi asp.ambur,

    Based on your description, you printed repeater without border alignment.

    Then you could refer to the following code:

    <script type = "text/javascript">
            function PrintPanel() {
                var panel = document.getElementById("<%=Panel1.ClientID %>");
                var printWindow = window.open('', '', 'height=400,width=800');
                printWindow.document.write('<html><head><title>DIV Contents</title>');
                printWindow.document.write('</head><body >');
                printWindow.document.write(panel.innerHTML);
                printWindow.document.write('</body></html>');
                printWindow.document.close();
                setTimeout(function () {
                    printWindow.print();
                }, 500);
                return false;
            }
        </script>
    
    <asp:Panel ID="Panel1" runat="server">
        <div >
            <table id="tbl" border="1" width="200px" style="border-collapse:collapse;">
                <asp:Repeater runat="server" ID="Repeater1">
                   <ItemTemplate>
                       <tr><td><%#Eval("Id")%></td>
                           <td><%#Eval("userName")%></td>
                           <td><%#Eval("userDetail")%></td></td>
                       </tr>
                   </ItemTemplate>
                    </asp:Repeater>
            </table>
        </div>
                </asp:Panel>
            <asp:Button ID="Button1" runat="server" Text="print repeater" OnClientClick = "return PrintPanel();" />

    Output:

    Best regards,

    Cathy

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, July 19, 2017 7:28 AM
  • User2033107836 posted

    Thank You Cathy Zou

    Thanks Lot For Your Code

    Wednesday, July 19, 2017 8:59 AM
  • User2033107836 posted

    Hello

    Print dailogue coming perfect but <td> height width not coming perfect as its looking before printing button click 

    why

    Wednesday, July 19, 2017 9:09 AM
  • User-707554951 posted

    Hi asp.ambur,

    I use the following code to change my height and width and it works.

    <table id="tbl" border="1" style="border-collapse:collapse;width:200px;height:400px;">

    So if you still have problem, please give me your code for test.

    Best regards,

    Cathy

    Thursday, July 20, 2017 1:47 AM