2011-02-27 81 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); 
      } 
     } 
    } 
}