0

我有一个从数据库中检索的对象,其中一个字段是 URL。我在网格视图中显示这些数据,我希望 URL 列是一系列链接,上面写着“下载”,它指向相应的 URL。

我目前有以下内容:

<asp:GridView ID="my_gv" runat="server" AutoGenerateColumns = "false" 
            GridLines="None" Width="100%" AllowSorting="True" 
            CssClass="table table-bordered table-condensed">
            <AlternatingRowStyle BackColor="#F5F5F5" />
            <Columns>
                <asp:HyperLinkField  DataNavigateUrlFields = "location" Text = "Download link" HeaderText = "Download"  />
            </Columns>    
        </asp:GridView>

这似乎不起作用。条目像链接一样是蓝色的,但是用鼠标悬停实际上不会调用 URL(光标不会变为“链接”光标)。查看生成的 HTML,它们是<a>标签,但不包含href属性。为什么?我错过了什么才能让它发挥作用?我填充以下内容:

        private void populateElementView()
        {
            List<MyElement> elements = database.getGeneratedElements();
            // elements has a .location property
            my_gv.DataSource = elements;
            my_gv.DataBind();

        }

有关详细信息,URL 指向计算机上的文件。手动插入带有 url “testing” 的项目实际上是有效的,而实际的文件路径却没有。

4

2 回答 2

1

试试这个可能对你有帮助。在这里,我使用DataNavigateUrlFormatString 属性,用于

Gets or sets the string that specifies the format in which the URLs for the hyperlinks in a HyperLinkField object are rendered.

<asp:HyperLinkField  DataNavigateUrlFields = "location" Text = "Download link" HeaderText = "Download" DataNavigateUrlFormatString="{0}" />

编辑 :

您可以将文件放在服务器位置,并且您的位置字段将具有这样的虚拟路径MusicFiles/File1.avi

现在您可以将您的网址格式化HyperLinkFieldhttp://localhost//Download/{0}{0} 表示 MusicFiles/File1.avi 或您location的字段值

<asp:HyperLinkField  DataNavigateUrlFields = "location" Text = "Download link" HeaderText = "Download" DataNavigateUrlFormatString="http://localhost//Download/{0}" />

我认为这将在您部署它时对您有所帮助,并且用户可以从您的服务器下载文件。

于 2013-03-12T13:44:51.450 回答
0

试试这个方法:

<asp:HyperLink ID="HyperLink1" Text="Download link" NavigateUrl='<%# Eval("location")%>' runat="server"></asp:HyperLink>

或者,如果您使用另一个控件,只需通过这种方式将值绑定到 NavigateUrl:

<%# Eval("location")%>
于 2013-03-12T13:44:17.830 回答