Solved

How do I delete file listed in a gridview?

Posted on 2008-10-24
7
215 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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 

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

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

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

There is an easy way, in .NET, to centralize the treatment of all unexpected errors. First of all, instead of launching the application directly in a Form, you need first to write a Sub called Main, in a module. Then, set the Startup Object to th…
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…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.

739 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