2014-11-05 78 views
0

我在ASP.NET中工作。我有一個gridview,它的列2 hyperlinks(其餘爲regulardatafield)。 他們看起來像:將gridview導出爲PDF時超鏈接字段爲空

<asp:TemplateField HeaderText="Costumer"> 
      <ItemTemplate> 
       <asp:HyperLink ID="HyperLink1" runat="server" 
        NavigateUrl='<%# Eval("CUSTOMER_ID", "javascript:void(window.open(&#039;CustSubsDetailsPage.aspx?CUSTOMER_ID={0}&#039;,&#039;&#039;,&#039; width=500, height=500, top=100, left=100&#039;))") %>' 
        Text='<%# Eval("CUSTOMER_ID") %>'></asp:HyperLink> 
      </ItemTemplate> 
     </asp:TemplateField> 

在CS代碼我只使用數據綁定,然後導出到pdf。 一切工作正常,除了那些空的2列。

編輯這裏要求是PDF文件中的代碼:

protected void btnExportPDF_Click(object sender, EventArgs e) 
{ 
    GridView.AllowPaging = false; 
    GridView.DataBind(); 

    iTextSharp.text.pdf.PdfPTable table = new iTextSharp.text.pdf.PdfPTable(GridView.Columns.Count); 
    table.WidthPercentage = 90; 
    table.RunDirection = PdfWriter.RUN_DIRECTION_RTL; 

    for (int i = 0; i < GridView.Columns.Count; i++) 
    { 
     string cellText = headers[i]; 
     BaseFont bf = BaseFont.CreateFont("C:\\Windows\\Fonts\\Arial.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED); 
     iTextSharp.text.Font font = new iTextSharp.text.Font(bf, 6, iTextSharp.text.Font.NORMAL, BaseColor.BLACK); 
     iTextSharp.text.pdf.PdfPCell cell = new iTextSharp.text.pdf.PdfPCell(new Phrase(8, cellText, font)); 
     table.AddCell(cell); 
    } 
    for (int i = 0; i < GridView.Rows.Count; i++) 
    { 
     if (GridView.Rows[i].RowType == DataControlRowType.DataRow) 
     { 
      for (int j = 0; j < GridView.Columns.Count; j++) 
      { 
       string cellText = Server.HtmlDecode(GridView.Rows[i].Cells[j].Text); 
       BaseFont bf = BaseFont.CreateFont("C:\\Windows\\Fonts\\Arial.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED); 
       iTextSharp.text.Font font = new iTextSharp.text.Font(bf, 6, iTextSharp.text.Font.NORMAL, BaseColor.BLACK); 
       iTextSharp.text.pdf.PdfPCell cell = new iTextSharp.text.pdf.PdfPCell(new Phrase(8, cellText, font)); 
       table.AddCell(cell); 
      } 
     } 
    } 

    Document pdfDoc = new Document(PageSize.A4_LANDSCAPE, 10f, 10f, 10f, 0f); 
    PdfWriter.GetInstance(pdfDoc, Response.OutputStream); 
    pdfDoc.Open(); 
    pdfDoc.Add(table); 
    pdfDoc.Close(); 
    Response.ContentType = "application/pdf"; 
    Response.AddHeader("content-disposition", "attachment;" + "filename=Dlf_Log_report_" + DateTime.Now + ".pdf"); 
    Response.Cache.SetCacheability(HttpCacheability.NoCache); 
    Response.Write(pdfDoc); 
    Response.End(); 
} 

能否請你幫我找出這個問題? 在此先感謝。

+0

如何將'GridView'導出爲PDF?你能提供解決方案的源代碼嗎? – 2014-11-05 09:03:38

+0

@PavelTimoshenko我已經添加了代碼 – user3710346 2014-11-05 10:49:30

回答

0

屬性Text不能用於ItemTemplate。你應該對這樣的列使用以下代碼:

string cellText = Server.HtmlDecode((GridView.Rows[i].Cells[j].FindControl("hyperLinkId") as HyperLink).NavigateUrl); 
+0

謝謝!工作! – user3710346 2014-11-06 08:18:54

+0

我很高興能幫上忙。不要忘記標記回答的問題。 – 2014-11-06 09:23:29