Solved

How do I delete file listed in a gridview?

Posted on 2008-10-24
7
211 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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
When we want to run, execute or repeat a statement multiple times, a loop is necessary. This article covers the two types of loops in Python: the while loop and the for loop.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

920 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

15 Experts available now in Live!

Get 1:1 Help Now