Hi alhamd,
Refer below example.
HTML
C#
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="Text" HeaderText="FileName" />
<asp:TemplateField>
<ItemTemplate>
<audio controls style='display:<%# Eval("Value").ToString().ToLower() == ".mp3" ? "block" : "none" %>'>
<source src='<%# ResolveUrl(Eval("Text").ToString()) %>' type='audio/mpeg' />
</audio>
<video id="VideoPlayer" src="<%# ResolveUrl(Eval("Text").ToString()) %>" controls="true"
width="200" height="250" loop="true" style='display:<%# Eval("Value").ToString().ToLower() == ".mp4" ? "block" : "none" %>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
VB.Net
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="Text" HeaderText="FileName" />
<asp:TemplateField>
<ItemTemplate>
<audio controls style='display: <%# If(Eval("Value").ToString().ToLower() = ".mp3", "block", "none") %>'>
<source src='<%# ResolveUrl(Eval("Text").ToString()) %>' type='audio/mpeg' />
</audio>
<video id="VideoPlayer" src='<%# ResolveUrl(Eval("Text").ToString()) %>' controls="true"
width="200" height="250" loop="true" style='display: <%# If(Eval("Value").ToString().ToLower() = ".mp4", "block", "none") %>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Namespaces
C#
using System.IO;
VB.Net
Imports System.IO
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
this.BindGrid();
}
}
private void BindGrid()
{
string[] filePaths = Directory.GetFiles(Server.MapPath("~/Files/"));
List<ListItem> files = new List<ListItem>();
foreach (string filePath in filePaths)
{
files.Add(new ListItem("~/Files/" + Path.GetFileName(filePath), Path.GetExtension(filePath)));
}
GridView1.DataSource = files;
GridView1.DataBind();
}
VB.Net
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
Me.BindGrid()
End If
End Sub
Private Sub BindGrid()
Dim filePaths As String() = Directory.GetFiles(Server.MapPath("~/Files/"))
Dim files As List(Of ListItem) = New List(Of ListItem)()
For Each filePath As String In filePaths
files.Add(New ListItem("~/Files/" + Path.GetFileName(filePath), Path.GetExtension(filePath)))
Next
GridView1.DataSource = files
GridView1.DataBind()
End Sub
Screenshot