0

I have a GridView which I export to Excel (export gridview to excel file) and it works.

But the problem is that the GridView contains a HyperLinkField column so its gets exported to excel as a URL while I want it to be as a text in Excel and not as a link, how do I do this?

The HyperLinkField column, in gridview, that I have is:

<asp:HyperLinkField DataNavigateUrlFields="learntable_id" DataNavigateUrlFormatString="~/page2.aspx?learntable_id={0}" HeaderText="Learning Description" DataTextField="learntable_desc" />

I tried to fix the problem with the following:

  • First I gave an ID to the url column so I can be able to work with that column in codebehind so I replace the Hyperlinkfield with a Hyperlink like this:

            <asp:TemplateField HeaderText="Learning Description">
                <ItemTemplate>
                    <asp:HyperLink id="hyperlinkcolumn" runat="server" NavigateUrl='<%# "~/page2.aspx?learntable_id=" & Eval("learntable_id") %>' text='<%# Eval("learntable_desc") %>' ></asp:HyperLink>
                </ItemTemplate>
            </asp:TemplateField>
    
  • and in the Export to Excel code in page behind I try to convert that column to a text but I fail and I am not sure if I do it right:

    For Each gr As GridViewRow In GridView1.Rows
        Convert.ToString(CType(gr.Cells(1).FindControl("hyperlinkcolumn").ToString, String))
    Next
    

But I can't arrive to change the link column to a text column, help please.

4

2 回答 2

1

您可以删除所有链接并将它们替换为文本控件。

这是 C# 代码-

private void RemoveLinks(Control grdView)
    {
        LinkButton lb = new LinkButton();
        Literal l = new Literal();

        for (int i = 0; i < grdView.Controls.Count; i++)
        {
            if (grdView.Controls[i].GetType() == typeof(LinkButton))  // or hyperlink
            {
                l.Text = (grdView.Controls[i] as LinkButton).Text;
                grdView.Controls.Remove(grdView.Controls[i]);
                grdView.Controls.AddAt(i, l);
            }
            if (grdView.Controls[i].HasControls())
            {
                RemoveLinks(grdView.Controls[i]);
            }
        }        
    }
于 2013-04-24T06:40:49.047 回答
1

您可以通过 JavaScript 实现这一点,通过 Id 从 Link 中删除下划线,或者为要从中删除下划线的 HyperLink 分配 Css 类。

使用链接 ID:

$(document).ready(function(){
  $('#HyperLinkId').css('textDecoration','none');
});

带有链接 CssClass:

$(document).ready(function(){
    $('.HyperlinkClass').css('textDecoration','none');
});

更新1:

您也可以从 HyperLink jQuery 中删除 href 属性:

使用链接 ID:

$(document).ready(function(){
    $('#HyperLinkId').removeAttr("href");
});

带有链接 CssClass:

$(document).ready(function(){
    $('.HyperlinkClass').removeAttr("href");
});
于 2013-04-24T07:29:04.630 回答