locked
I Want To Print My Data A4 Page Content RRS feed

  • Question

  • User-807418713 posted

    Hello

    This is my sql table with data

    USE [Purchase_Sales]
    GO
    /****** Object:  Table [dbo].[Table1]    Script Date: 11/27/2018 18:27:05 ******/
    SET ANSI_NULLS ON
    GO
    SET QUOTED_IDENTIFIER ON
    GO
    SET ANSI_PADDING ON
    GO
    CREATE TABLE [dbo].[Table1](
    	[ID] [int] IDENTITY(1,1) NOT NULL,
    	[Item_Name] [varchar](50) NULL,
    	[BC] [varchar](50) NULL,
     CONSTRAINT [PK_Table1] PRIMARY KEY CLUSTERED 
    (
    	[ID] ASC
    )WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
    ) ON [PRIMARY]
    GO
    SET ANSI_PADDING OFF
    GO
    SET IDENTITY_INSERT [dbo].[Table1] ON
    INSERT [dbo].[Table1] ([ID], [Item_Name], [BC]) VALUES (1, N'Apple', N'MS1118-001')
    INSERT [dbo].[Table1] ([ID], [Item_Name], [BC]) VALUES (2, N'Mango', N'MS1118-002')
    INSERT [dbo].[Table1] ([ID], [Item_Name], [BC]) VALUES (3, N'Orange', N'MS1118-003')
    INSERT [dbo].[Table1] ([ID], [Item_Name], [BC]) VALUES (4, N'Lemon', N'MS1118-004')
    INSERT [dbo].[Table1] ([ID], [Item_Name], [BC]) VALUES (5, N'Apple', N'MS1118-001')
    INSERT [dbo].[Table1] ([ID], [Item_Name], [BC]) VALUES (6, N'Mango', N'MS1118-002')
    INSERT [dbo].[Table1] ([ID], [Item_Name], [BC]) VALUES (7, N'Orange', N'MS1118-003')
    INSERT [dbo].[Table1] ([ID], [Item_Name], [BC]) VALUES (8, N'Lemon', N'MS1118-004')
    INSERT [dbo].[Table1] ([ID], [Item_Name], [BC]) VALUES (9, N'Apple', N'MS1118-001')
    INSERT [dbo].[Table1] ([ID], [Item_Name], [BC]) VALUES (10, N'Mango', N'MS1118-002')
    INSERT [dbo].[Table1] ([ID], [Item_Name], [BC]) VALUES (11, N'Orange', N'MS1118-003')
    INSERT [dbo].[Table1] ([ID], [Item_Name], [BC]) VALUES (12, N'Lemon', N'MS1118-004')
    INSERT [dbo].[Table1] ([ID], [Item_Name], [BC]) VALUES (13, N'Apple', N'MS1118-001')
    INSERT [dbo].[Table1] ([ID], [Item_Name], [BC]) VALUES (14, N'Mango', N'MS1118-002')
    INSERT [dbo].[Table1] ([ID], [Item_Name], [BC]) VALUES (15, N'Orange', N'MS1118-003')
    INSERT [dbo].[Table1] ([ID], [Item_Name], [BC]) VALUES (16, N'Lemon', N'MS1118-004')
    INSERT [dbo].[Table1] ([ID], [Item_Name], [BC]) VALUES (17, N'Apple', N'MS1118-001')
    INSERT [dbo].[Table1] ([ID], [Item_Name], [BC]) VALUES (18, N'Mango', N'MS1118-002')
    INSERT [dbo].[Table1] ([ID], [Item_Name], [BC]) VALUES (19, N'Orange', N'MS1118-003')
    INSERT [dbo].[Table1] ([ID], [Item_Name], [BC]) VALUES (20, N'Lemon', N'MS1118-004')
    INSERT [dbo].[Table1] ([ID], [Item_Name], [BC]) VALUES (21, N'Apple', N'MS1118-001')
    INSERT [dbo].[Table1] ([ID], [Item_Name], [BC]) VALUES (22, N'Mango', N'MS1118-002')
    INSERT [dbo].[Table1] ([ID], [Item_Name], [BC]) VALUES (23, N'Orange', N'MS1118-003')
    INSERT [dbo].[Table1] ([ID], [Item_Name], [BC]) VALUES (24, N'Lemon', N'MS1118-004')
    INSERT [dbo].[Table1] ([ID], [Item_Name], [BC]) VALUES (25, N'Apple', N'MS1118-001')
    INSERT [dbo].[Table1] ([ID], [Item_Name], [BC]) VALUES (26, N'Mango', N'MS1118-002')
    INSERT [dbo].[Table1] ([ID], [Item_Name], [BC]) VALUES (27, N'Orange', N'MS1118-003')
    INSERT [dbo].[Table1] ([ID], [Item_Name], [BC]) VALUES (28, N'Lemon', N'MS1118-004')
    INSERT [dbo].[Table1] ([ID], [Item_Name], [BC]) VALUES (29, N'Apple', N'MS1118-001')
    INSERT [dbo].[Table1] ([ID], [Item_Name], [BC]) VALUES (30, N'Mango', N'MS1118-002')
    INSERT [dbo].[Table1] ([ID], [Item_Name], [BC]) VALUES (31, N'Orange', N'MS1118-003')
    INSERT [dbo].[Table1] ([ID], [Item_Name], [BC]) VALUES (32, N'Lemon', N'MS1118-004')
    INSERT [dbo].[Table1] ([ID], [Item_Name], [BC]) VALUES (33, N'Apple', N'MS1118-001')
    INSERT [dbo].[Table1] ([ID], [Item_Name], [BC]) VALUES (34, N'Mango', N'MS1118-002')
    INSERT [dbo].[Table1] ([ID], [Item_Name], [BC]) VALUES (35, N'Orange', N'MS1118-003')
    INSERT [dbo].[Table1] ([ID], [Item_Name], [BC]) VALUES (36, N'Lemon', N'MS1118-004')
    INSERT [dbo].[Table1] ([ID], [Item_Name], [BC]) VALUES (37, N'Apple', N'MS1118-001')
    INSERT [dbo].[Table1] ([ID], [Item_Name], [BC]) VALUES (38, N'Mango', N'MS1118-002')
    INSERT [dbo].[Table1] ([ID], [Item_Name], [BC]) VALUES (39, N'Orange', N'MS1118-003')
    INSERT [dbo].[Table1] ([ID], [Item_Name], [BC]) VALUES (40, N'Lemon', N'MS1118-004')
    INSERT [dbo].[Table1] ([ID], [Item_Name], [BC]) VALUES (41, N'Apple', N'MS1118-001')
    INSERT [dbo].[Table1] ([ID], [Item_Name], [BC]) VALUES (42, N'Mango', N'MS1118-002')
    INSERT [dbo].[Table1] ([ID], [Item_Name], [BC]) VALUES (43, N'Orange', N'MS1118-003')
    INSERT [dbo].[Table1] ([ID], [Item_Name], [BC]) VALUES (44, N'Lemon', N'MS1118-004')
    INSERT [dbo].[Table1] ([ID], [Item_Name], [BC]) VALUES (45, N'Apple', N'MS1118-001')
    INSERT [dbo].[Table1] ([ID], [Item_Name], [BC]) VALUES (46, N'Mango', N'MS1118-002')
    INSERT [dbo].[Table1] ([ID], [Item_Name], [BC]) VALUES (47, N'Orange', N'MS1118-003')
    INSERT [dbo].[Table1] ([ID], [Item_Name], [BC]) VALUES (48, N'Lemon', N'MS1118-004')
    INSERT [dbo].[Table1] ([ID], [Item_Name], [BC]) VALUES (49, N'Apple', N'MS1118-001')
    INSERT [dbo].[Table1] ([ID], [Item_Name], [BC]) VALUES (50, N'Mango', N'MS1118-002')
    INSERT [dbo].[Table1] ([ID], [Item_Name], [BC]) VALUES (51, N'Orange', N'MS1118-003')
    INSERT [dbo].[Table1] ([ID], [Item_Name], [BC]) VALUES (52, N'Lemon', N'MS1118-004')
    INSERT [dbo].[Table1] ([ID], [Item_Name], [BC]) VALUES (53, N'Apple', N'MS1118-001')
    INSERT [dbo].[Table1] ([ID], [Item_Name], [BC]) VALUES (54, N'Mango', N'MS1118-002')
    INSERT [dbo].[Table1] ([ID], [Item_Name], [BC]) VALUES (55, N'Orange', N'MS1118-003')
    INSERT [dbo].[Table1] ([ID], [Item_Name], [BC]) VALUES (56, N'Lemon', N'MS1118-004')
    INSERT [dbo].[Table1] ([ID], [Item_Name], [BC]) VALUES (57, N'Apple', N'MS1118-001')
    INSERT [dbo].[Table1] ([ID], [Item_Name], [BC]) VALUES (58, N'Mango', N'MS1118-002')
    INSERT [dbo].[Table1] ([ID], [Item_Name], [BC]) VALUES (59, N'Orange', N'MS1118-003')
    INSERT [dbo].[Table1] ([ID], [Item_Name], [BC]) VALUES (60, N'Lemon', N'MS1118-004')
    INSERT [dbo].[Table1] ([ID], [Item_Name], [BC]) VALUES (61, N'Apple', N'MS1118-001')
    INSERT [dbo].[Table1] ([ID], [Item_Name], [BC]) VALUES (62, N'Mango', N'MS1118-002')
    INSERT [dbo].[Table1] ([ID], [Item_Name], [BC]) VALUES (63, N'Orange', N'MS1118-003')
    INSERT [dbo].[Table1] ([ID], [Item_Name], [BC]) VALUES (64, N'Lemon', N'MS1118-004')
    INSERT [dbo].[Table1] ([ID], [Item_Name], [BC]) VALUES (65, N'Apple', N'MS1118-001')
    INSERT [dbo].[Table1] ([ID], [Item_Name], [BC]) VALUES (66, N'Mango', N'MS1118-002')
    INSERT [dbo].[Table1] ([ID], [Item_Name], [BC]) VALUES (67, N'Orange', N'MS1118-003')
    INSERT [dbo].[Table1] ([ID], [Item_Name], [BC]) VALUES (68, N'Lemon', N'MS1118-004')
    INSERT [dbo].[Table1] ([ID], [Item_Name], [BC]) VALUES (69, N'Apple', N'MS1118-001')
    INSERT [dbo].[Table1] ([ID], [Item_Name], [BC]) VALUES (70, N'Mango', N'MS1118-002')
    INSERT [dbo].[Table1] ([ID], [Item_Name], [BC]) VALUES (71, N'Orange', N'MS1118-003')
    INSERT [dbo].[Table1] ([ID], [Item_Name], [BC]) VALUES (72, N'Lemon', N'MS1118-004')
    SET IDENTITY_INSERT [dbo].[Table1] OFF
    

    this is my aspx page

    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Test_BC.aspx.cs" Inherits="NN_JC" %>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head id="Head1" runat="server">
        <title>New Job Card</title>
        
    <style>
         .border {
                 box-sizing:border-box;
                    border:1px solid grey;
                    
            }
            
            body {
        margin: 0;
        padding: 0;
        background-color: #FAFAFA;
        font: 12pt "Tahoma";
    }
    * {
        box-sizing: border-box;
        -moz-box-sizing: border-box;
    }
    .page {
        width: 21cm;
        min-height: 29.7cm;
        padding: 0.4cm;
        margin: 1cm auto;
        border: 1px #D3D3D3 solid;
        border-radius: 5px;
        background: white;
        box-shadow: 0 0 5px rgba(0, 0, 0, 0.1);
    }
    .subpage {
           margin: 0.2cm auto;
        border: 1px red solid;
         width: 5cm;
         height: 2cm;
         
    }
    
     
     
        
        </style>
    </head>
    
    <body style="font-size: 12pt; font-family: Times New Roman">
        <form id="form1" runat="server">
        <div>
       
        
       
     
            <table>
                <tr>
                    <td colspan="5" rowspan="2">
                        <asp:Label ID="Label4" runat="server" Font-Bold="True" Font-Names="Palatino Linotype"
                            Font-Size="12pt" ForeColor="Red" Style="text-align: center" Text="Todays Date"
                            Width="265px"></asp:Label><asp:TextBox ID="TextBox2" runat="server" autocomplete="off" Width="103px"></asp:TextBox>
                        <asp:Button ID="Button3" runat="server" Height="43px" OnClientClick = "return PrintDiv();"
                         Style="background-image: url(images/NewPrint.png);
                            background-color: transparent" Width="113px" OnClick="Button3_Click" /><asp:Button ID="Search" runat="server" BackColor="Transparent" BorderColor="Transparent"
                            BorderStyle="Dashed" BorderWidth="1px" Font-Bold="True" Font-Names="Palatino Linotype"
                            Font-Size="10pt" ForeColor="#FFFFFF" Height="40px" OnClick="Search_Click" onmouseover="return CheckVal();"
                            Style="background-image: url(images/btn_5.png); background-color: transparent"
                            Text="Search" ValidationGroup="a" Width="94px" /><asp:Button ID="Button2" runat="server" AccessKey="R" BackColor="Transparent" BorderColor="Transparent"
                            BorderStyle="Dashed" BorderWidth="1px" Font-Bold="True" Font-Names="Palatino Linotype"
                            Font-Size="10pt" ForeColor="#FFFFFF" Height="40px" OnClick="refresh_Click" Style="background-image: url(images/btn_5.png)"
                            Text="Refresh" Width="94px" />&nbsp;
                    </td>
                </tr>
                <tr>
                </tr>
            </table>
          
         
      <div id="dvContents" class="page">
        
        <asp:Repeater ID="Repeater1" runat="server" >
                
                 <ItemTemplate>  
                 
                   
                     
                     <div style="float:left;text-align:center" class="subpage">
                        
                       
                       <asp:Label ID="Label2" runat="server" Font-Size="10px" Font-Names="Palatino Linotype" Text='<%# Bind("Item_Name") %>'></asp:Label>
                       <br />
                       <asp:Label ID="Label1" runat="server"  Font-Names="MRV Code39extMA" Text='<%# Bind("BC") %>'></asp:Label>
    
                    
                      </div>       
           
                   
                                                                           
                </ItemTemplate>
             </asp:Repeater>
              
     </div>
          
           
            <br />
             
            &nbsp;&nbsp;<br />
            &nbsp;
            <br />
            <br />
            <br />
            <br />
            <br />
            <br />
        </div>
        </form>
    </body>
    </html>
    

    this is my code behind

    using System;
    using System.Data;
    using System.Configuration;
    using System.Collections;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;
    using System.Data.SqlClient;
    using System.IO;
    
    public partial class NN_JC : System.Web.UI.Page
    {
    
        
    
        private void bindrepeater()
        {
           
                DataSet dt = new DataSet();
                SqlConnection con1 = new SqlConnection(ConfigurationManager.ConnectionStrings["CHEMIMSConnectionString"].ConnectionString);
                con1.Open();
                SqlCommand cmd1 = new SqlCommand("select Item_Name,BC from Table1", con1);
                SqlDataAdapter ada1 = new SqlDataAdapter(cmd1);
                ada1.Fill(dt);
                Repeater1.DataSource = dt;
                Repeater1.DataBind();
                con1.Close();
            
        }
    
     
    
        protected void Page_Init(object sender, EventArgs e)
        {
           
        }
    
    
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                TextBox2.Text = DateTime.Now.ToString("dd-MMM-yyyy");
    
               bindrepeater();
    
             
            }
        }
        protected void Button1_Click(object sender, EventArgs e)
        {
            
        }
        
        protected void Search_Click(object sender, EventArgs e)
        {
            bindrepeater();
        }
        protected void refresh_Click(object sender, EventArgs e)
        {
            Response.Redirect(Request.RawUrl);
        }
        protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        {
          
        }
    }
    

    I want to print A4 page above code on button click 

    problem its not coming second page also i need on button click data print directly to my printer

    give javascript code 

    Thank you

    Tuesday, November 27, 2018 12:14 PM

Answers

  • User-893317190 posted

    Hi Gopi.MCA,

    You could use window.print() to print your html page.

     <script>
            function PrintDiv() {
                    window.print()
                    return false;
                }
            </script>

    If you use chrome, you could then click change and click manage to change the printer.

    You could also directly change the settings of chrome, in search settings , enter print and then change the printer.

    Best regards,

    Ackerly Xu

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, November 28, 2018 3:10 AM

All replies

  • User-893317190 posted

    Hi Gopi.MCA,

    You could use window.print() to print your html page.

     <script>
            function PrintDiv() {
                    window.print()
                    return false;
                }
            </script>

    If you use chrome, you could then click change and click manage to change the printer.

    You could also directly change the settings of chrome, in search settings , enter print and then change the printer.

    Best regards,

    Ackerly Xu

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, November 28, 2018 3:10 AM
  • User-807418713 posted

    Hello Ackerly Xu

    Thanks for your code

    Copy and paste my code and please check the data of second page is not perfect align also please check the print should be done for repeater only not for all page

    need right code

    Thank You

    Wednesday, November 28, 2018 6:40 AM
  • User-893317190 posted

    Hi Gopi.MCA,

    I find your table doesn't contain all the div in the repearter, you could change the height of the table using css.

    .page {
        width: 21cm;
        min-height: 45cm;
        padding: 0.4cm;
        margin: 1cm auto;
        border: 1px #D3D3D3 solid;
        border-radius: 5px;
        background: white;
        box-shadow: 0 0 5px rgba(0, 0, 0, 0.1);
    }

    About only printing the data in repeater, you could refer to the link below, make your repeater shows in a single widow using widow.open and then use window.print.

    https://forums.asp.net/t/1978704.aspx?Print+Repeater+in+ASP+NET

    Best regards,

    Ackerly Xu

    Thursday, November 29, 2018 1:11 AM