How to delete a file using a button in GridView

Posted on 2012-09-21
Medium Priority
Last Modified: 2012-09-23
I populated a <asp:GridView ID="GridView1" with a ist of files.
I want to add a "delete" button to the gridview, which when clicked would delete that file.
Question by:MikeCombe
  • 3
  • 3
LVL 15

Expert Comment

by:Ess Kay
ID: 38422745
add a column, set the column type to button

example:  http://stackoverflow.com/questions/3062733/gridview-changing-text-in-a-button-column
LVL 15

Accepted Solution

Ess Kay earned 500 total points
ID: 38422749
when someone click it add code to delete file
where filename path =  column("filename")

Author Comment

ID: 38422878
I have the button....just trying to figure out how to pass the specific filename....

<asp:Button OnClick="MyBtnHandler" Text="Delete" CommandArgument="<%# DataBinder.Eval(Container, 'DataItem.Name') %>" CommandName="Delete" name="btnDelete" ID="btnDelete" runat="server" Width="100" />
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.


Author Comment

ID: 38422883
Here's the gridview code....

<asp:TemplateField headertext="" HeaderStyle-HorizontalAlign="Left">
            <asp:Button OnClick="MyBtnHandler" Text="Delete" CommandArgument="<%# DataBinder.Eval(Container, 'DataItem.Name') %>" CommandName="Delete" name="btnDelete" ID="btnDelete" runat="server" Width="100" />                  
LVL 18

Assisted Solution

by:Rajar Ahmed
Rajar Ahmed earned 500 total points
ID: 38424030
try this,

  Protected Sub btnText_Click(sender As Object, e As EventArgs)
        Dim lnkbtn As Button = TryCast(sender, Button)
        'getting particular row linkbutton
        Dim gvrow As GridViewRow = TryCast(lnkbtn.NamingContainer, GridViewRow)
        'getting userid of particular row
        Dim userid As Integer = Convert.ToInt32(gvData.DataKeys(gvrow.RowIndex).Value.ToString())
        Dim filename As String = gvrow.Cells(1).Text//This should be your filename 
Dim completePath As String = Server.MapPath("~/PDF/Document/" & filename & ".pdf")//This should be your folder location where the file exists
If System.IO.File.Exists(completePath) Then
End If

    End Sub

Open in new window


Author Closing Comment

ID: 38424061
actually, it just takes this..

        Imports System.IO

        Dim myFilename as String = "test.jpg"
        Dim myFolder As String = "C:\xxxx\"
        File.Delete(myFolder & myFilename)
LVL 15

Expert Comment

by:Ess Kay
ID: 38426229
Gretchen I could help

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Lots of people ask this question on how to extend the “MembershipProvider” to make use of custom authentication like using existing database or make use of some other way of authentication. Many blogs show you how to extend the membership provider c…
One of the pain points with developing AJAX, JavaScript, JQuery, and other client-side behaviors is that JavaScript doesn’t allow for cross domain request for pulling content. For example, JavaScript code on www.johnchapman.name could not pull conte…
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
Suggested Courses
Course of the Month13 days, 16 hours left to enroll

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question