Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

How do I delete file listed in a gridview?

Posted on 2008-10-24
7
Medium Priority
?
218 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 2000 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
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.

 

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

Docker-Compose to Simplify Multi-Container Builds

Our veteran DevOps Author takes you through how to build a multi-container environment, managed with a single utility in order to simplify your deployments.

Question has a verified solution.

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

Having just graduated from college and entered the workforce, I don’t find myself always using the tools and programs I grew accustomed to over the past four years. However, there is one program I continually find myself reverting back to…R.   So …
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 viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
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…

721 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