超链接字段为空,而出口的GridView为PDF [英] Hyperlink field is empty while exporting gridview to PDF

查看:168
本文介绍了超链接字段为空,而出口的GridView为PDF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在ASP.NET工作。我有一个 GridView控件,致使其2是列超链接(其余为 regulardatafield )。
他们看起来像:

I work in ASP.NET. I have a gridview, that 2 of it's columns are hyperlinks (the others are regulardatafield). they look like that:

 <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 code我只使用数据绑定,然后导出到 PDF
一切正常perfectrly除2列是空的。

In the cs code i only use databind, and then export to pdf. Everything works perfectrly except those 2 columns that are empty.

修改这里要求的是code为PDF文件:

EDIT as requested here is the code for the pdf file:

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();
}

能否请你帮我找出这个问题?
先谢谢了。

Can you please help me figure out the problem? Thanks in advance.

推荐答案

属性文本不能用于的ItemTemplate 。您应该使用以下code对这些列:

Property Text cannot be used for ItemTemplate. You should use the following code for such columns:

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

这篇关于超链接字段为空,而出口的GridView为PDF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆