How to display a List of <string> URLs as Hyperlinks on an ASP.Net Page (C #)

I have a list of lines where each line is the URL of the PDF document. All I want to do is loop through this list and display each URL as a hyperlink on my page. I saw this before in MVC, where the collection is viewable, and you can just do foreach, etc., but I don’t know how to do it on a regular asp.net page ...

Any help is appreciated, welcomed!

+3
source share
2 answers

You can create links using the ASP repeater on the page and link your list to it.

Page:

<asp:Repeater id="repLinks" runat="server">
   <ItemTemplate>
      <asp:HyperLink runat="server" NavigateUrl='<%# Container.DataItem.ToString() %>' Text="LinkText" />
   </ItemTemplate>
</asp:Repeater>

Code for:

List<string> lLinks = new List<string>();

//Define your list contents here

repLinks.DataSource = lLinks;
repLinks.DataBind();

Something like this should do the trick.

+12
<%
foreach(var url in Urls)
{
    %><a href="<%=url%>"><%=url%></a><%
}
%>
+2

Source: https://habr.com/ru/post/1745966/


All Articles