在visual studios 2010中添加gridview中的下载超链接 [英] Add a download Hyperlink in gridview in visual studios 2010

查看:217
本文介绍了在visual studios 2010中添加gridview中的下载超链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题很简单,我想将gridview的最后一列作为我计算机本地文件的下载链接。

My problem is simple, I would like to make the last column of my gridview to be a download link to a file in local on my computer.

我的数据表有3列:

User_id, request_id, FilePath

FilePath是我计算机上文件的路径(字符串),我去过的地方尝试是:
在我的网页上的gridview中显示2个第一列(User_id和request_id)。
在第3列,我添加了一个超链接字段,并尝试将其(使用属性DataNavigateUrlField)链接到列FilePath的内容。

FilePath is the path (string) to the file on my computer, what I've been trying is : In my gridview on the webpage display the 2 first columns (User_id and request_id). On the 3rd column I added a hyperlink field and tried to link it (with the property DataNavigateUrlField) to the content of the column FilePath.

我是结果是我甚至无法点击的死链接,他们只是在鼠标悬停时改变颜色。

I've ended up with dead links on which i can't even click, they just change color on mouseover.

任何人都有这方面的线索?非常感谢

Any one have a clue about that ? Thanks a lot

推荐答案

可能的解决方案是使用 TemplateField 对于您的超链接列:

A possible solution could be to use a TemplateField for your Hyperlink column:

<asp:GridView AutoGenerateColumns="false">
    <Columns>
        <asp:TemplateField>
            <ItemTemplate>
                <a href='<%# DataBinder.Eval(Container.DataItem, "FilePath") %>'>
                    <%# DataBinder.Eval(Container.DataItem, "FilePath")%>
                </a>
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

将创建所需的输出:

<tr>
    <td>
        <a href='c:/directory/file.xxx'>c:/directory/file.xxx</a>
    </td>
</tr>

似乎 HyperLinkField 不接受其 DataNavigateUrlFields 属性的文件路径。

It seems that a HyperLinkField does not accept a file path for its DataNavigateUrlFieldsproperty.

我试过通过设置 DataNavigateUrlFormatString 属性来智取它:

I tried to outsmart it by setting the DataNavigateUrlFormatString property:

<asp:HyperLinkField DataNavigateUrlFormatString="file:///{0}" DataNavigateUrlFields="FilePath" DataTextField="FilePath" />

但问题仍然存在且只会产生

but the issue remains and will only produce

<tr>
    <td>
        <a>c:/directory/file.xxx</a>
    </td>
</tr>

这篇关于在visual studios 2010中添加gridview中的下载超链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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