2011-02-27 68 views
5

我使用Visual Studio 2008和SQL Server 2008中网格视图在asp.net表打印

我想在asp.net使用按钮 3.5 代码必须打印我的“与表的GridView”第三部分第一,这是从我的默认页面

protected void btnPrint_Click(object sender, EventArgs e) 
{ 
    Session["ctrl"] = Panel1; 
    ClientScript.RegisterStartupScript(this.GetType(), "onclick", 
     "<script language=javascript>window.open('Print.aspx','PrintMe','height=300px,width=300px,scrollbars=1');</script>"); 
    PrintHelper.PrintWebControl(Panel1); 
} 

这个代码这个从打印页面

protected void Page_Load(object sender, EventArgs e) 
{ 
    Control ctrl = (Control)Session["ctrl"]; 
    PrintHelper.PrintWebControl(ctrl); 
} 

这是我的打印辅助类

public PrintHelper() 
{ 
} 

public static void PrintWebControl(Control ctrl) 
{ 
    PrintWebControl(ctrl, string.Empty); 
} 

public static void PrintWebControl(Control ctrl, string Script) 
{ 
    StringWriter stringWrite = new StringWriter(); 
    System.Web.UI.HtmlTextWriter htmlWrite = new System.Web.UI.HtmlTextWriter(stringWrite); 
    if (ctrl is WebControl) 
    { 
     Unit w = new Unit(100, UnitType.Percentage); ((WebControl)ctrl).Width = w; 
    } 
    Page pg = new Page(); 
    pg.EnableEventValidation = false; 
    if (Script != string.Empty) 
    { 
     pg.ClientScript.RegisterStartupScript(pg.GetType(), "PrintJavaScript", Script); 
    } 
    HtmlForm frm = new HtmlForm(); 
    pg.Controls.Add(frm); 
    frm.Attributes.Add("runat", "server"); 
    frm.Controls.Add(ctrl); 
    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(); 
} 

请帮我

+0

这个问题太模糊了。需要更多的细节:哪里,什么...... – onof 2011-02-27 15:43:31

+0

我已经创建了一个网页,打印我的网格视图,它显示了gridview中的数据,但问题是它并没有显示我的网格视图的行和列 – CHANDRAHAS 2011-02-27 15:49:36

+0

你能告诉我们你到目前为止的代码,所以我们没有猜测? – RQDQ 2011-02-27 16:00:08

回答

1

创建一个数据适配器,创建一个Command对象以选择查询,设置适配器的命令,此命令对象,问题的适配器。填充(数据集),将gridview的数据源设置为数据集并对GridView进行数据绑定。

对于代码,可以使用简单的Google搜索。你甚至没有指定一种语言。我猜你正在使用C#,这是你的大学项目。是吗?

+0

是的,你的权利,其网上银行项目我想打印银行的声明,但我不想使用水晶报告我的数据源工作正常,它获取数据frm数据库问题是,我想在打印页面中看到表和行 – CHANDRAHAS 2011-02-27 15:58:06

+0

在这种情况下,创建一个新的打印功能,并使用它重定向到只有网格视图的页面。如果你已经看到了足够多的银行网站,你会发现银行通常会把它放在一个JavaScript中,它会打开一个新的弹出窗口,只有gridview表和一个打印按钮。尤其是印度铁路网站。在任何时候,您都应该考虑Crystal Reports,因为它是.NET中的一项新功能,并且会为您的项目增加相当大的价值。 – r3st0r3 2011-02-28 02:52:29

1

首先,让你的数据显示在GridView中(这里有大约bazillion的资源和其他向你展示如何做到这一点)。

这里的打印窗口的JavaScript:

<INPUT TYPE="button" onClick="window.print()"> 
+0

但这将打印整个页面,我只需要我的数据gridview打印 – CHANDRAHAS 2011-02-27 16:01:36

+1

然后,你将不得不建立一个页面,只有你的数据gridview。 – RQDQ 2011-02-28 02:23:51

2

这也许可以提供帮助。

公共类GridViewExportUtil {

public static void Export(string fileName, GridView gv) 
    { 
     HttpContext.Current.Response.Clear(); 
     HttpContext.Current.Response.ClearHeaders(); 
     HttpContext.Current.Response.ClearContent(); 
     HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("windows-1254"); 
     HttpContext.Current.Response.Charset = "windows-1254"; //ISO-8859-13 ISO-8859-9 windows-1254 

     HttpContext.Current.Response.AddHeader(
      "content-disposition", string.Format("attachment; filename={0}", fileName)); 
     HttpContext.Current.Response.ContentType = "application/ms-excel"; 
     string header = "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\n<html xmlns=\"http://www.w3.org/1999/xhtml\">\n<head>\n<title></title>\n<meta http-equiv=\"Content-Type\" content=\"text/html; charset=windows-1254\" />\n<style>\n</style>\n</head>\n<body>\n"; 

     using (StringWriter sw = new StringWriter()) 
     { 
      using (HtmlTextWriter htw = new HtmlTextWriter(sw)) 
      { 
       // Create a form to contain the grid 
       Table table = new Table(); 
       table.GridLines = GridLines.Horizontal; 
       //table.CellSpacing = 17; 

       // add the header row to the table 
       if (gv.HeaderRow != null) 
       { 
        GridViewExportUtil.PrepareControlForExport(gv.HeaderRow); 
        table.Rows.Add(gv.HeaderRow); 
       } 

       // add each of the data rows to the table 
       foreach (GridViewRow row in gv.Rows) 
       { 
        GridViewExportUtil.PrepareControlForExport(row); 
        table.Rows.Add(row); 
       } 

       // add the footer row to the table 
       if (gv.FooterRow != null) 
       { 
        GridViewExportUtil.PrepareControlForExport(gv.FooterRow); 

        table.Rows.Add(gv.FooterRow); 
        //table.Height = 17; 
       } 

       // render the table into the htmlwriter 
       table.RenderControl(htw); 

       // render the htmlwriter into the response 
       HttpContext.Current.Response.Write(header + sw.ToString()); 
       HttpContext.Current.Response.End(); 
      } 
     } 
    } 

    /// <summary> 
    /// Replace any of the contained controls with literals 
    /// </summary> 
    /// <param name="control"></param> 
    private static void PrepareControlForExport(Control control) 
    { 
     for (int i = 0; i < control.Controls.Count; i++) 
     { 
      Control current = control.Controls[i]; 
      if (current is LinkButton) 
      { 
       control.Controls.Remove(current); 
       control.Controls.AddAt(i, new LiteralControl((current as LinkButton).Text)); 
      } 
      else if (current is ImageButton) 
      { 
       control.Controls.Remove(current); 
       control.Controls.AddAt(i, new LiteralControl((current as ImageButton).AlternateText)); 
      } 
      else if (current is HyperLink) 
      { 
       control.Controls.Remove(current); 
       control.Controls.AddAt(i, new LiteralControl((current as HyperLink).Text)); 
      } 
      else if (current is DropDownList) 
      { 
       control.Controls.Remove(current); 
       control.Controls.AddAt(i, new LiteralControl((current as DropDownList).SelectedItem.Text)); 
      } 
      else if (current is CheckBox) 
      { 
       control.Controls.Remove(current); 
       control.Controls.AddAt(i, new LiteralControl((current as CheckBox).Checked ? "True" : "False")); 
      } 
      else if (current is Label) 
      { 
       control.Controls.Remove(current); 
       control.Controls.AddAt(i, new LiteralControl((current as Label).Text)); 
      } 

      if (current.HasControls()) 
      { 
       GridViewExportUtil.PrepareControlForExport(current); 
      } 
     } 
    } 
}