Hi ashish007,
Check this example. Now please take its reference and correct your code.
HTML
<asp:DataList ID="Datalistpost" runat="server" Width="560px" BorderWidth="1px" DataKeyField="st_id" RepeatLayout="Table" RepeatColumns="2">
<ItemStyle CssClass="grid1" />
<ItemTemplate>
<table style="width: 546px">
<tr>
<td colspan="2" align="center">
<asp:Image ID="im11" runat="server" ImageUrl='<%#Eval("FILE_PATH") %>' Width="300px"
Style="margin-top: 20px;" Height="400px" /><br />
<br />
<video id="VideoPlayer" src='<%#Eval("FILE_PATH") %>' runat="server" controls="true"
width="300" height="200" type="video/mp4">
</video>
</td>
</tr>
<tr>
<td colspan="2">
<asp:Label ID="LBLSTID" runat="server" Text='<%#Eval("st_id") %>' Visible="false"></asp:Label>
</td>
</tr>
<tr>
<td colspan="2" align="center">
<asp:Button ID="BTNDISCARD" runat="server" Text="Discard" CssClass="btnpost" CommandName="Delete"
CausesValidation="false" />
<asp:Button ID="BTNSAHRE" runat="server" Text="Share Story" CssClass="btnpost" CommandName="SHARE"
CommandArgument='<%#Eval("st_id") %>' CausesValidation="false" />
</td>
</tr>
</table>
</ItemTemplate>
</asp:DataList>
Namespaces
C#
using System.Data;
using System.IO;
using System.Web.UI.HtmlControls;
VB.Net
Imports System.Data
Imports System.IO
Imports System.Web.UI.HtmlControls
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
show_story();
}
}
private void show_story()
{
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[] { new DataColumn("st_id"), new DataColumn("FILE_PATH") });
dt.Rows.Add(1, "Files/1.jpg");
dt.Rows.Add(2, "Files/1.mp4");
dt.Rows.Add(3, "Files/2.jpg");
DataSet odt = new DataSet();
odt.Tables.Add(dt);
//odt = IP.showuploadstory(Int32.Parse(Session["uid"].ToString()));
if (odt != null)
{
if (odt.Tables[0].Rows.Count > 0)
{
Datalistpost.DataSource = odt;
Datalistpost.DataBind();
foreach (DataListItem item in Datalistpost.Items)
{
Image im = item.FindControl("im11") as Image;
if (Path.GetExtension(im.ImageUrl) != ".jpg")
{
im.Visible = false;
}
else
{
im.Visible = true;
im.ImageUrl = "~/" + im.ImageUrl;
}
HtmlGenericControl vdo = item.FindControl("VideoPlayer") as HtmlGenericControl;
string ak = vdo.Attributes["src"].ToString();
if (Path.GetExtension(ak) == ".mp4")
{
vdo.Visible = true;
vdo.Attributes["src"] = ak;
}
else
{
vdo.Visible = false;
}
}
}
}
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not IsPostBack Then
show_story()
End If
End Sub
Private Sub show_story()
Dim dt As DataTable = New DataTable()
dt.Columns.AddRange(New DataColumn() {New DataColumn("st_id"), New DataColumn("FILE_PATH")})
dt.Rows.Add(1, "Files/1.jpg")
dt.Rows.Add(2, "Files/1.mp4")
dt.Rows.Add(3, "Files/2.jpg")
Dim odt As DataSet = New DataSet()
odt.Tables.Add(dt)
If odt IsNot Nothing Then
If odt.Tables(0).Rows.Count > 0 Then
Datalistpost.DataSource = odt
Datalistpost.DataBind()
For Each item As DataListItem In Datalistpost.Items
Dim im As Image = TryCast(item.FindControl("im11"), Image)
If Path.GetExtension(im.ImageUrl) <> ".jpg" Then
im.Visible = False
Else
im.Visible = True
im.ImageUrl = "~/" & im.ImageUrl
End If
Dim vdo As HtmlGenericControl = TryCast(item.FindControl("VideoPlayer"), HtmlGenericControl)
Dim ak As String = vdo.Attributes("src").ToString()
If Path.GetExtension(ak) = ".mp4" Then
vdo.Visible = True
vdo.Attributes("src") = ak
Else
vdo.Visible = False
End If
Next
End If
End If
End Sub
Screenshot