title: Printing an ASP.NET Web Site page
body: <p>I have spent 5 days researching how to print a DataList in a C# Visual Studio WEB SITE.  I can print the DataList in a Web Project.  It appears that since a Site does not have "namespace" as does a Project, Microsoft just didn't bother making a Web Site page printable.  I have a rather large Web Site almost developed, and now it appears a major function of a Site is not possible.  I need help!  Thanks in advance.</p>
  
  <p>P.S.  It would seem that Microsoft would have mentioned this little problem in their literature comparing a Project to a Site, but they didn't. </p>
  
  <pre><code>&lt;%@ Page Title="" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true"
   CodeBehind="PageLinkDetails.aspx.cs" Inherits="TestDataBase.PageLinkDetails" %&gt;
  
  &lt;%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="asp" %&gt;
  &lt;asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" runat="server"&gt;
  &lt;/asp:Content&gt;
  &lt;asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server"&gt;
  &lt;div&gt;
      &lt;asp:Panel ID="pnl1" runat="server"&gt;
          &lt;asp:SqlDataSource runat="server" ID="SqlDataSource1" ConnectionString="&lt;%$ ConnectionStrings:Caribbean %&gt;"
              SelectCommand="SELECT * FROM [CaribbeanDirections] WHERE ([RecipeID] = @RecipeID)"&gt;
              &lt;SelectParameters&gt;
                  &lt;asp:QueryStringParameter Name="RecipeID" QueryStringField="RecipeID" Type="Int32" /&gt;
              &lt;/SelectParameters&gt;
          &lt;/asp:SqlDataSource&gt;
          &lt;asp:DataList ID="DataList1" runat="server" DataKeyField="DirectionID" DataSourceID="SqlDataSource1"&gt;
              &lt;ItemTemplate&gt;
                  &lt;asp:Label ID="NameLabel" runat="server" Text='&lt;%# Eval("Name") %&gt;' /&gt;
                  &lt;br /&gt;
                  &lt;br /&gt;
                  INGREDIENTS:&lt;br /&gt;
                  &lt;asp:Label ID="IngredientOneLabel" runat="server" Text='&lt;%# Eval("IngredientOne") %&gt;' /&gt;
                  &lt;br /&gt;
                  &lt;asp:Label ID="IngredientTwoLabel" runat="server" Text='&lt;%# Eval("IngredientTwo") %&gt;' /&gt;
                  &lt;br /&gt;
                  &lt;asp:Label ID="IngredientThreeLabel" runat="server" Text='&lt;%# Eval("IngredientThree") %&gt;' /&gt;
                  &lt;br /&gt;
                  &lt;asp:Label ID="IngredientFourLabel" runat="server" Text='&lt;%# Eval("IngredientFour") %&gt;' /&gt;
                  &lt;br /&gt;
                  &lt;asp:Label ID="IngredientFiveLabel" runat="server" Text='&lt;%# Eval("IngredientFive") %&gt;' /&gt;
                  &lt;br /&gt;
                  &lt;asp:Label ID="IngredientSixLabel" runat="server" Text='&lt;%# Eval("IngredientSix") %&gt;' /&gt;
                  &lt;br /&gt;
                  &lt;asp:Label ID="IngredientSevenLabel" runat="server" Text='&lt;%# Eval("IngredientSeven") %&gt;' /&gt;
                  &lt;br /&gt;
                  &lt;asp:Label ID="IngredientEightLabel" runat="server" Text='&lt;%# Eval("IngredientEight") %&gt;' /&gt;
                  &lt;br /&gt;
                  &lt;asp:Label ID="IngredientNineLabel" runat="server" Text='&lt;%# Eval("IngredientNine") %&gt;' /&gt;
                  &lt;br /&gt;
                  &lt;asp:Label ID="IngredientTenLabel" runat="server" Text='&lt;%# Eval("IngredientTen") %&gt;' /&gt;
                  &lt;br /&gt;
                  Directions:&lt;br /&gt;
                  &lt;asp:Label ID="DirectionOneLabel" runat="server" Text='&lt;%# Eval("DirectionOne") %&gt;' /&gt;
                  &lt;br /&gt;
                  &lt;asp:Label ID="DirectionTwoLabel" runat="server" Text='&lt;%# Eval("DirectionTwo") %&gt;' /&gt;
                  &lt;br /&gt;
                  &lt;asp:Label ID="DirectionThreeLabel" runat="server" Text='&lt;%# Eval("DirectionThree") %&gt;' /&gt;
                  &lt;br /&gt;
                  &lt;asp:Label ID="DirectionFourLabel" runat="server" Text='&lt;%# Eval("DirectionFour") %&gt;' /&gt;
                  &lt;br /&gt;
                  &lt;br /&gt;
              &lt;/ItemTemplate&gt;
          &lt;/asp:DataList&gt;
      &lt;/asp:Panel&gt;
      &lt;asp:Button ID="btnPrintCurrent" runat="server" Text="Print Current Page" OnClick="PrintCurrentPage" /&gt;
     &lt;/div&gt;
     &lt;asp:HyperLink ID="HyperLink2" runat="server" NavigateUrl='&lt;%# Bind("RecipeID",      "PageLinkDetails.aspx?RecipeId={0}") %&gt;'
      Text='&lt;%# Eval("Name") %&gt;'&gt;Rate This Recipe&lt;/asp:HyperLink&gt;
    &lt;/asp:Content&gt;
  
  
  
  using System;
  using System.Data;
  using System.Data.SqlClient;
  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.IO;
   using System.Text;
   using System.Web.SessionState;
  
  namespace TestDataBase
  {
  public partial class PageLinkDetails : System.Web.UI.Page
  {
      protected void Page_Load(object sender, EventArgs e)
      {
          Response.Cache.SetCacheability(HttpCacheability.NoCache);
          BindGrid();
      }
  
      private void BindGrid()
      {
          string strQuery = "select * " + "from CaribbeanDirections";
          DataTable dt = new DataTable();
          String strConnString = System.Configuration.ConfigurationManager
                      .ConnectionStrings["Caribbean"].ConnectionString;
          SqlConnection con = new SqlConnection(strConnString);
          SqlDataAdapter sda = new SqlDataAdapter();
          SqlCommand cmd = new SqlCommand(strQuery);
          cmd.CommandType = CommandType.Text;
          cmd.Connection = con;
          try
          {
              con.Open();
              sda.SelectCommand = cmd;
              sda.Fill(dt);
              //*DataList1.DataSource = dt;
              DataList1.DataBind();
          }
          catch (Exception ex)
          {
              throw ex;
          }
          finally
          {
              con.Close();
              sda.Dispose();
              con.Dispose();
          }
      }
      public override void VerifyRenderingInServerForm(Control control)
      {
          /*Verifies that the control is rendered */
      }
  
      protected void OnPaging(object sender, PageEventArgs e)
      {
  
          DataList1.DataBind();
      }
  
      protected void PrintCurrentPage(object sender, EventArgs e)
      {
  
          DataList1.DataBind();
          StringWriter sw = new StringWriter();
          HtmlTextWriter hw = new HtmlTextWriter(sw);
          DataList1.RenderControl(hw);
          string gridHTML = sw.ToString().Replace("\"", "'")
              .Replace(System.Environment.NewLine, "");
          StringBuilder sb = new StringBuilder();
          sb.Append("&lt;script type = 'text/javascript'&gt;");
          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(gridHTML);
          sb.Append("\");");
          sb.Append("printWin.document.close();");
          sb.Append("printWin.focus();");
          sb.Append("printWin.print();");
          sb.Append("printWin.close();};");
          sb.Append("&lt;/script&gt;");
          ClientScript.RegisterStartupScript(this.GetType(), "GridPrint", sb.ToString());
  
          DataList1.DataBind();
      }
  
      protected void btnPrint_Click(object sender, EventArgs e)
      {
          Session["ctrl"] = pnl1;
  
      }
  }
  }
  </code></pre>