Solved

How do I delete file listed in a gridview?

Posted on 2008-10-24
7
217 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

 

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

Understanding Linux Permissions

Linux for beginners: How to view the permissions associated with files and directories and also how you can change them.

Question has a verified solution.

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

The purpose of this article is to demonstrate how we can use conditional statements using Python.
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

617 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