Solved

How do I delete file listed in a gridview?

Posted on 2008-10-24
7
210 Views
Last Modified: 2012-05-05
I have a gridview that is loaded with file names populated by Microsoft Indexing Service.  The files are located in a directory accessible at "~/PDF files/".  I want to add the ability to deleted a file from the directory. The markup and the code behind for the gridview is provided in the code snippet section.  

I am unable to get the correct value for "fileName"; it should be the file name shown on the row of the gridview.  Any suggestion on how to fix my code?  
<asp:GridView ID="GridView1" runat="server" 

    AutoGenerateColumns="false">

    <Columns>

         <asp:TemplateField ShowHeader="False">

            <ItemTemplate>

                <table width="100%" >

                    <tr>

                        <td style="width:50px;">

                            <asp:ImageButton ID="DeleteImageButton" runat="server" ImageUrl="~/Images/delete.png" CommandName="Delete" />

                        </td>

                        <td>

                            <asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl='<%# Eval("FilePath") %>' Text='<%# Eval("Name") %>' />

                        </td>

                    </tr>

                </table>

            </ItemTemplate>

        </asp:TemplateField>

    </Columns>

</asp:GridView>
 
 

Private Sub GridView1_RowDeleting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewDeleteEventArgs) Handles GridView1.RowDeleting

    Dim fileName As String = e.Values.Item(1).ToString()

    If File.Exists("~/PDF Files/" + fileName) Then

        File.Delete("~/PDF Files/" + fileName)

    End If

End Sub

Open in new window

0
Comment
Question by:seckel
  • 3
  • 3
7 Comments
 
LVL 15

Expert Comment

by:jorge_toriz
ID: 22797314
File.Exists(Server.MapPath("~/PDF Files/" + fileName))
File.Delete(Server.MapPath("~/PDF Files/" + fileName))
0
 

Author Comment

by:seckel
ID: 22797350
The problem is not with using the File.Delete method, but with getting the correct fileName from the gridview...

Dim fileName As String = e.Values.Item(1).ToString()

This code is not getting the correct fileName from the gridview...
0
 
LVL 15

Accepted Solution

by:
jorge_toriz earned 500 total points
ID: 22797487
Pass the fileName as an argument and get the argument in your deleting event

<asp:ImageButton ID="DeleteImageButton" runat="server" ImageUrl="~/Images/delete.png" CommandName="Delete" CommandArgument='<%# Eval("Name") %>' />
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

by:seckel
ID: 22797599
What woud the code behind look like?

When I try to add an arguement to the GridView1_RowDeleting method, the event handle becomes invalid.
0
 
LVL 15

Expert Comment

by:jorge_toriz
ID: 22797647
No, in your GridViewDeleteEventArgs you must have a property like "Argument"... in that property you will have your fileName
0
 

Author Comment

by:seckel
ID: 22797842
The changes in the code snippet section works.


Markup:

CommandArgument='<%# Eval("Name") %>' OnCommand="DeleteLink"
 

Code Behind:

Protected Sub DeleteLink(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.CommandEventArgs)
 

        Dim fileName As String = e.CommandArgument.ToString
 

        If File.Exists(Server.MapPath("~/PDF Files/" + fileName)) Then

            File.Delete(Server.MapPath("~/PDF Files/" + fileName))

        End If
 

    End Sub

Open in new window

0
 
LVL 26

Expert Comment

by:Anurag Thakur
ID: 22797856
add
DataKeyNames=Name, OnRowDeleting="GridView_RowDeleting", OnRowCommand="GridView_RowCommand"
in the gridview definition section

in the code behind do the following
protected void GridView_RowDeleting (object sender, GridViewDeleteEventArgs e)
{
}

protected void GridView_RowCommand (object sender, GridViewCommandEventArgs e)
{
      string fileName = gridView1.DataKeys[Convert.ToInt32 (e.CommandArgument)].Value.ToString ();
      // move your delete logic here from your RowDeleting method and then rebind the grid
}
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

If you haven’t already, I encourage you to read the first article (http://www.experts-exchange.com/articles/18680/An-Introduction-to-R-Programming-and-R-Studio.html) in my series to gain a basic foundation of R and R Studio.  You will also find the …
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

706 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now