【问题标题】:Hyperlink field is empty while exporting gridview to PDF将 gridview 导出为 PDF 时超链接字段为空
【发布时间】:2015-01-01 08:11:03
【问题描述】:

我在 ASP.NET 中工作。我有一个gridview,其中两列是hyperlinks(其他是regulardatafield)。 它们看起来像这样:

 <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代码中我只使用databind,然后导出到pdf。 除了那两列是空的之外,一切都完美无缺。

EDIT 这里要求的是 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();
}

你能帮我找出问题吗? 提前致谢。

【问题讨论】:

  • 如何将GridView 导出为PDF?你能提供你的解决方案的源代码吗?
  • @PavelTimoshenko 我已经添加了代码

标签: c# asp.net pdf gridview hyperlink


【解决方案1】:

属性Text 不能用于ItemTemplate。您应该对此类列使用以下代码:

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

【讨论】:

  • 很高兴能帮上忙。不要忘记将问题标记为已回答。
猜你喜欢
  • 2014-01-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-10-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多