using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;
namespace DemoPrintTextBoxValuesInLabel
{
public partial class WebForm1 : System.Web.UI.Page
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString);
protected void Page_Load(object sender, EventArgs e)
{
bindCombo();
}
public void bindCombo()
{
con.Open();
SqlCommand cmd = new SqlCommand("Select * from UserInfomation ", con);
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = cmd;
DataTable dt = new DataTable();
da.Fill(dt);
DropDownList1.DataSource = dt;
DropDownList1.DataBind();
DropDownList1.DataValueField = "UserName";
DropDownList1.DataTextField = "UserName";
DropDownList1.DataBind();
con.Close();
}
protected void Print(object sender, EventArgs e)
{
string html = string.Empty;
using (StreamReader reader = new StreamReader(Server.MapPath("~/PrintPage.html")))
{
html = reader.ReadToEnd();
}
html = html.Replace("{Name}", txtName.Text);
html = html.Replace("{City}", txtCity.Text);
html = html.Replace("{Phone}", txtPhone.Text);
html = html.Replace("{combo}", DropDownList1.Text);
this.dvHtml.InnerHtml = html;
int startIndex = html.IndexOf("table");
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
this.dvHtml.RenderControl(hw);
string divHTML = 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(divHTML);
sb.Append("\");");
sb.Append("printWin.document.close();");
sb.Append("printWin.focus();");
sb.Append("printWin.print();");
sb.Append("printWin.close();};");
sb.Append("</script>");
ClientScript.RegisterStartupScript(this.GetType(), "GridPrint", sb.ToString());
}
}
}