Delete posted file from Session on Button Click inside ASP.Net ListView using C# and VB.Net

Last Reply 5 months ago By pandeyism

Posted 5 months ago

how to delete uploaded file in listview control on button click in asp.net c#

 

Posted 5 months ago

Hi anirudhp,

Refer below sample.

HTML

<div>
    <asp:FileUpload ID="fuMultiple" runat="server" multiple="multiple" />
    <br />
    <asp:Button Text="Upload" runat="server" OnClick="Upload" />
    <asp:Button Text="Save" runat="server" OnClick="Save" />
    <br />
    <asp:ListView runat="server" ID="lstview1">
        <ItemTemplate>
            <table border="1" cellpadding="0" cellspacing="0">
                <tr>
                    <td>
                        <asp:Label ID="Label1" Text='<%#Eval("Text") %>' runat="server" />
                    </td>
                    <td>
                        <asp:LinkButton Text="Delete" runat="server" OnClick="Delete" CommandArgument='<%#Container.DataItemIndex %>' />
                    </td>
                </tr>
            </table>
        </ItemTemplate>
    </asp:ListView>
</div>

Namespaces

C#

using System.IO;

VB.Net

Imports System.IO

Code

C#

protected void Upload(object sender, EventArgs e)
{
    List<HttpPostedFile> files = new List<HttpPostedFile>();
    for (int i = 0; i < Request.Files.Count; i++)
    {
        files.Add(Request.Files[i]);
    }
    Session["Files"] = files;
    BindFiles(files);
}

private void BindFiles(List<HttpPostedFile> files)
{
    lstview1.DataSource = (from f in files
                            select new
                            {
                                Text = f.FileName
                            }).ToList();
    lstview1.DataBind();
}

protected void Save(object sender, EventArgs e)
{
    List<HttpPostedFile> files = Session["Files"] as List<HttpPostedFile>;
    for (int i = 0; i < files.Count; i++)
    {
        files[i].SaveAs(Server.MapPath("~/uploads/") + Path.GetFileName(files[i].FileName));
    }
}

protected void Delete(object sender, EventArgs e)
{
    List<HttpPostedFile> files = Session["Files"] as List<HttpPostedFile>;
    int index = int.Parse((sender as LinkButton).CommandArgument);
    files.RemoveAt(index);
    Session["Files"] = files;
    BindFiles(files);
}

VB.Net

Protected Sub Upload(ByVal sender As Object, ByVal e As EventArgs)
    Dim files As List(Of HttpPostedFile) = New List(Of HttpPostedFile)()
    For i As Integer = 0 To Request.Files.Count - 1
        files.Add(Request.Files(i))
    Next
    Session("Files") = files
    BindFiles(files)
End Sub

Private Sub BindFiles(ByVal files As List(Of HttpPostedFile))
    lstview1.DataSource = (From f In files Select New With {Key .Text = f.FileName}).ToList()
    lstview1.DataBind()
End Sub

Protected Sub Save(ByVal sender As Object, ByVal e As EventArgs)
    Dim files As List(Of HttpPostedFile) = TryCast(Session("Files"), List(Of HttpPostedFile))

    For i As Integer = 0 To files.Count - 1
        files(i).SaveAs(Server.MapPath("~/uploads/") + Path.GetFileName(files(i).FileName))
    Next
End Sub

Protected Sub Delete(ByVal sender As Object, ByVal e As EventArgs)
    Dim files As List(Of HttpPostedFile) = TryCast(Session("Files"), List(Of HttpPostedFile))
    Dim index As Integer = Integer.Parse((TryCast(sender, LinkButton)).CommandArgument)
    files.RemoveAt(index)
    Session("Files") = files
    BindFiles(files)
End Sub

Screenshot