如何操作数据库结果以添加到Gridview的链接?

时间:2022-04-30 07:15:25

I have a GridView that I use to display the results of a query that I craft and execute in a asp.net code-behind file. The trouble is, I have one of the columns return a rather long URL. I would rather display this is the gridview as an image or text with an anchor tag wrapped around it, with the original link.

我有一个GridView,用于显示我在asp.net代码隐藏文件中创建并执行的查询结果。问题是,我有一个列返回一个相当长的URL。我宁愿显示这是gridview作为一个图像或文本,在它周围有一个锚标记,带有原始链接。

How do I do this such that I have the same image on every row of the column, but different URLs depending on what was originally returned to me?

如何做到这一点,使我在列的每一行上都有相同的图像,但根据最初返回给我的内容不同,url不同?

Thanks for looking.

谢谢你看。

1 个解决方案

#1


1  

 <asp:GridView ID="GridView1" runat="server"  DataSourceID="SqlDataSource1" >
        <Columns>
            <asp:HyperLinkField HeaderText="Header"
               Text ="TextYouWantToAppearOnColumn"
               DataNavigateUrlField="FieldNameFromDataSource" 
               DataNavigateUrlFormatString="http://{0}" />
        </Columns>
  </asp:GridView>

If hyperlink is enough for you without image, above solution should work for you. {0} is the value from database, you can format the url in any way you want. In this case, I assumed it is an external url to the website without htpp://, that is why i added it.

如果超链接对您来说已经足够了,那么上面的解决方案应该适合您。{0}是来自数据库的值,您可以以任何您想要的方式格式化url。在这种情况下,我假设它是没有htpp的网站的外部url://,这就是为什么我添加了它。

#1


1  

 <asp:GridView ID="GridView1" runat="server"  DataSourceID="SqlDataSource1" >
        <Columns>
            <asp:HyperLinkField HeaderText="Header"
               Text ="TextYouWantToAppearOnColumn"
               DataNavigateUrlField="FieldNameFromDataSource" 
               DataNavigateUrlFormatString="http://{0}" />
        </Columns>
  </asp:GridView>

If hyperlink is enough for you without image, above solution should work for you. {0} is the value from database, you can format the url in any way you want. In this case, I assumed it is an external url to the website without htpp://, that is why i added it.

如果超链接对您来说已经足够了,那么上面的解决方案应该适合您。{0}是来自数据库的值,您可以以任何您想要的方式格式化url。在这种情况下,我假设它是没有htpp的网站的外部url://,这就是为什么我添加了它。