Add dynamic hyperlink on the fly in ASP.NET

S

sweetpotatop

Hello,

I would like to create dynamic hyperlinks in a web page when it is
first loaded by using ASP.NET. Basically there will be a list of
documents in a folder and I will list them all as a hyperlink for user
access.

I wonder how I can go through the folder and create the dynamic
hyperlink on the fly in ASP.NET.

Thanks in advance.
 
T

Teemu Keiski

Hi,

something like
***listfolder.aspx***

<%@ Page Language="VB" %>
<%@ Import Namespace="System.IO" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">

Protected Sub Page_Load(ByVal sender As Object, ByVal e As
System.EventArgs)
If Not Page.IsPostBack Then
'lists files in current folder
rptFiles.DataSource = Directory.GetFiles(Server.MapPath("."))
rptFiles.DataBind()

End If
End Sub

Protected Sub rptFiles_ItemDataBound(ByVal sender As Object, ByVal e As
System.Web.UI.WebControls.RepeaterItemEventArgs)
If e.Item.ItemType = ListItemType.Item OrElse e.Item.ItemType =
ListItemType.AlternatingItem Then
Dim strFile As String = Path.GetFileName(CStr(e.Item.DataItem))
Dim hplFile As HyperLink =
DirectCast(e.Item.FindControl("hplFile"), HyperLink)
hplFile.Text = strFile
hplFile.NavigateUrl = strFile
End If
End Sub
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Repeater ID="rptFiles" runat="server"
OnItemDataBound="rptFiles_ItemDataBound">
<ItemTemplate>
<asp:HyperLink ID="hplFile" runat="server" /> <br />
</ItemTemplate>
</asp:Repeater>
</div>
</form>
</body>
</html>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top