设置一个gridview列以包含链接,其中绑定的数据是URL的列表 [英] Setting a gridview column to contain links, where the bound data is a list of URLs
问题描述
我有一个从数据库检索的对象,其中的一个字段是URL.我正在gridview中显示此数据,我希望URL列是一系列链接,上面写着下载",它指向相应的URL.
I have an object that I retrieve from a database, where one of the fields is a URL. I am displaying this data in a gridview, and I'd like the URL column to be a series of links saying "Download", where it points to the corresponding 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(光标不会更改为"link"光标).查看生成的HTML,它们是< a>
标记,但它们不包含 href
属性.为什么?我错过了什么才能使它正常工作?我填充以下内容:
This doesn't appear to be working. The entries are blue like a link, but hovering over with a mouse doesn't actually invoke a URL (the cursor does not change to the "link" cursor). Looking at the generated HTML, they are <a>
tags but they contain no href
attrivute. Why? What have I missed to allow this to work? I populate with the following:
private void populateElementView()
{
List<MyElement> elements = database.getGeneratedElements();
// elements has a .location property
my_gv.DataSource = elements;
my_gv.DataBind();
}
有关更多信息,URL指向计算机上的文件.手动插入带有测试" URL的项目实际上是可行的,而实际的文件路径却无效.
For further information, the URLs point to files on a computer. Hand-inserting an item with the url "testing" actually works, while the actual file paths do not.
推荐答案
只需尝试一下,可能会对您有所帮助.在这里,我正在使用 DataNavigateUrlFormatString属性用于
Just try this may be it will help you. Here I am using DataNavigateUrlFormatString Property which is used for
获取或设置一个字符串,该字符串指定呈现HyperLinkField对象中超链接的URL的格式.
<asp:HyperLinkField DataNavigateUrlFields = "location" Text = "Download link" HeaderText = "Download" DataNavigateUrlFormatString="{0}" />
您可以将文件放在服务器位置,并且位置字段将具有虚拟路径,例如 MusicFiles/File1.avi
You can put your files on server location and your location field will have virtual path like this MusicFiles/File1.avi
现在,您可以将 HyperLinkField
中的网址格式设置为 http://localhost//Download/{0}
,其中{0}表示MusicFiles/File1.avi或您 location
字段值
Now you can format your url in HyperLinkField
as http://localhost//Download/{0}
where {0} means MusicFiles/File1.avi or you location
field value
<asp:HyperLinkField DataNavigateUrlFields = "location" Text = "Download link" HeaderText = "Download" DataNavigateUrlFormatString="http://localhost//Download/{0}" />
我认为这将在您部署它时为您提供帮助,并且用户可以从服务器上下载文件.
I think this will help you when you deploy it and user can download the files from your server.
这篇关于设置一个gridview列以包含链接,其中绑定的数据是URL的列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!