In this article I will explain how we can print only the DataList items which are selected using ASP.Net CheckBox.
Database
For this tutorial I have used the Microsoft NorthWind Database. You can download the link below.
 
HTML Markup
Below I have an ASP.Net DataList control displaying contact information from the Customers Table of the Northwind Database and a Button control that will print the DataList Selected Items.
<form id="form1" runat="server">
    <asp:DataList ID="dlContacts" runat="server" RepeatLayout="Table" RepeatColumns="3"
        CellPadding="2" CellSpacing="2">
        <ItemTemplate>
            <table cellpadding="2" cellspacing="0" border = "1" style="width: 200px; height: 100px; border: dashed 2px #04AFEF;background-color: #B0E2F5">
                <tr>
                    <td><asp:CheckBox ID="CheckBox1" runat="server" />
                    <b><u><%# Eval("ContactName") %></u></b></td>
                </tr>
                <tr>
                    <td>
                        <b>City: </b><%# Eval("City") %><br />
                        <b>Postal Code: </b><%# Eval("PostalCode") %><br />
                        <b>Country: </b><%# Eval("Country")%><br />
                        <b>Phone: </b><%# Eval("Phone")%><br />
                        <b>Fax: </b><%# Eval("Fax")%><br />
                    </td>
                </tr>
            </table>
        </ItemTemplate>
    </asp:DataList>
    <br />
    <asp:Button ID="btnPrint" runat="server" Text="Print" OnClick = "Print" />
</form>
 
 
Namespaces
You will need to import the following namespaces.
C#
using System.IO;
using System.Data;
using System.Configuration;
using System.Data.SqlClient;
using System.Collections.Generic;
 
VB.Net
Imports System.IO
Imports System.Data
Imports System.Text
Imports System.Data.SqlClient
Imports System.Collections.Generic
 
 
Binding the ASP.Net DataList Control
C#
protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        dlContacts.DataSource = this.GetData();
        dlContacts.DataBind();
    }
}
 
private DataTable GetData()
{
    string conString = ConfigurationManager.ConnectionStrings["conStr"].ConnectionString;
    using (SqlConnection con = new SqlConnection(conString))
    {
        using (SqlCommand cmd = new SqlCommand("select top 6 * from customers"))
        {
            cmd.Connection = con;
            cmd.CommandType = CommandType.Text;
            using (SqlDataAdapter sda = new SqlDataAdapter(cmd))
            {
                DataSet ds = new DataSet();
                sda.Fill(ds);
                return ds.Tables[0];
            }
        }
    }
}
 
VB.Net
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
    If Not IsPostBack Then
        dlContacts.DataSource = Me.GetData()
        dlContacts.DataBind()
    End If
End Sub
 
Private Function GetData() As DataTable
    Dim conString As String = ConfigurationManager.ConnectionStrings("conStr").ConnectionString
    Using con As New SqlConnection(conString)
        Using cmd As New SqlCommand("select top 6 * from customers")
           cmd.Connection = con
           cmd.CommandType = CommandType.Text
           Using sda As New SqlDataAdapter(cmd)
                Dim ds As New DataSet()
                sda.Fill(ds)
                Return ds.Tables(0)
           End Using
        End Using
    End Using
End Function
 
 
Printing the ASP.Net DataList Selected Items
The following event handler is executed when the Print button is clicked. It checks for  checked or selected items in the ASP.Net DataList control and removes the unchecked or items which are not selected and then rebinds the DataList control. Finally it prints the DataList content.
C#
protected void Print(object sender, EventArgs e)
{
    DataTable dt = GetData();
    List<DataRow> rows = new List<DataRow>();
    foreach (DataListItem item in dlContacts.Items)
    {
        if (!(item.FindControl("CheckBox1") as CheckBox).Checked)
        {
            rows.Add(dt.Rows[item.ItemIndex]);
        }
    }
    foreach (DataRow row in rows)
    {
        dt.Rows.Remove(row);
    }
    dlContacts.DataSource = dt;
    dlContacts.DataBind();
    foreach (DataListItem item in dlContacts.Items)
    {
        item.FindControl("CheckBox1").Visible = false;
    }
    StringWriter sw = new StringWriter();
    HtmlTextWriter hw = new HtmlTextWriter(sw);
    dlContacts.RenderControl(hw);
    string html = sw.ToString().Replace("\"", "'").Replace(System.Environment.NewLine, "");
    StringBuilder sb = new StringBuilder();
    sb.Append("<script type = 'text/javascript'>");
    sb.Append("window.onload = new function(){");
    sb.Append("var printWin = window.open('', '', 'left=0");
    sb.Append(",top=0,width=1000,height=600,status=0');");
    sb.Append("printWin.document.write(\"");
    sb.Append(html);
    sb.Append("\");");
    sb.Append("printWin.document.close();");
    sb.Append("printWin.focus();");
    sb.Append("printWin.print();printWin.opener.location.href=printWin.opener.location.href;");
    sb.Append("printWin.close();};");
    sb.Append("</script>");
    ClientScript.RegisterStartupScript(this.GetType(), "GridPrint", sb.ToString());
}
 
VB.Net
Protected Sub Print(sender As Object, e As EventArgs)
    Dim dt As DataTable = GetData()
    Dim rows As New List(Of DataRow)()
    For Each item As DataListItem In dlContacts.Items
        If Not TryCast(item.FindControl("CheckBox1"), CheckBox).Checked Then
            rows.Add(dt.Rows(item.ItemIndex))
        End If
    Next
    For Each row As DataRow In rows
        dt.Rows.Remove(row)
    Next
    dlContacts.DataSource = dt
    dlContacts.DataBind()
    For Each item As DataListItem In dlContacts.Items
        item.FindControl("CheckBox1").Visible = False
    Next
    Dim sw As New StringWriter()
    Dim hw As New HtmlTextWriter(sw)
    dlContacts.RenderControl(hw)
    Dim html As String = sw.ToString().Replace("""", "'").Replace(System.Environment.NewLine, "")
    Dim sb As New StringBuilder()
    sb.Append("<script type = 'text/javascript'>")
    sb.Append("window.onload = new function(){")
    sb.Append("var printWin = window.open('', '', 'left=0")
    sb.Append(",top=0,width=1000,height=600,status=0');")
    sb.Append("printWin.document.write(""")
    sb.Append(html)
    sb.Append(""");")
    sb.Append("printWin.document.close();")
    sb.Append("printWin.focus();")
    sb.Append("printWin.print();printWin.opener.location.href=printWin.opener
.location.href;"
)
    sb.Append("printWin.close();};")
    sb.Append("</script>")
    ClientScript.RegisterStartupScript(Me.[GetType](), "GridPrint", sb.ToString())
End Sub
 
 
Demo
 
 
Downloads