• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 384
  • Last Modified:

Displaying and deleteing files over the web

Hello all:

I am a newbie.  That being said I have managed to come across code that will display files in a particular foder and will have delete buttons next to them.  The only thing missing is "to create an event handler for the DataGrid's DeleteCommand event. In this event handler, we need to determine the file to delete and then delete it via the File.Delete() method."

Since I know almost night about .NET, I am at a loss here.

I have included the code below.

Any help will be greatly appreciated.

--D.

<%@ Import Namespace="System.IO" %>
<script language="VB" runat="server">
  Sub Page_Load(sender as Object, e as EventArgs)
    If Not Page.IsPostBack then
      Dim dirInfo as New DirectoryInfo(Server.MapPath(""))
   
      articleList.DataSource = dirInfo.GetFiles("*.aspx")
      articleList.DataBind()
    End If
  End Sub
 
  Sub articleList_ItemDataBound(sender as Object, e as DataGridItemEventArgs)
    ' First, make sure we're NOT dealing with a Header or Footer row
    If e.Item.ItemType <> ListItemType.Header AND _
         e.Item.ItemType <> ListItemType.Footer then
      'Now, reference the Button control that the Delete ButtonColumn
      'has been rendered to
      Dim deleteButton as Button = e.Item.Cells(0).Controls(0)

      'We can now add the onclick event handler
      deleteButton.Attributes("onclick") = "javascript:return " & _
                 "confirm('Are you sure you want to delete the file " & _
                 DataBinder.Eval(e.Item.DataItem, "Name") & "?')"    
    End If
  End Sub
 
 
  Sub articleList_DeleteFile(sender as Object, e as DataGridCommandEventArgs)
    'First, get the filename to delete
    Dim fileName as String = articleList.DataKeys(e.Item.ItemIndex)

    lblMessage.Text = "You opted to delete the file " & _
        fileName & ".<br />" & _
        "This file could be deleted by calling: " & _
        "<code>File.Delete(fileName)</code><p>"
   
    'You would want to rebind the Directory's files to the DataGrid after
    'deleting the file...
  End Sub
</script>

<form runat="server">
  <asp:label runat="server" id="lblMessage" Font-Italic="True" ForeColor="Red" />
  <asp:DataGrid runat="server" id="articleList" Font-Name="Verdana"
      AutoGenerateColumns="False" AlternatingItemStyle-BackColor="#eeeeee"
      HeaderStyle-BackColor="Navy" HeaderStyle-ForeColor="White"
      HeaderStyle-Font-Size="15pt" HeaderStyle-Font-Bold="True"
      DataKeyField="FullName"
      OnItemDataBound="articleList_ItemDataBound"
      OnDeleteCommand="articleList_DeleteFile">
    <Columns>
      <asp:ButtonColumn Text="Delete" ButtonType="PushButton"
              CommandName="Delete" />
      <asp:HyperLinkColumn DataNavigateUrlField="Name"
              DataTextField="Name" HeaderText="File Name" />
      <asp:BoundColumn DataField="LastWriteTime"
              HeaderText="Last Write Time"
              ItemStyle-HorizontalAlign="Center"
              DataFormatString="{0:d}" />
      <asp:BoundColumn DataField="Length" HeaderText="File Size"
                    ItemStyle-HorizontalAlign="Right"
                    DataFormatString="{0:#,### bytes}" />
    </Columns>
  </asp:DataGrid>
</form>
 
0
driven13
Asked:
driven13
  • 3
  • 3
1 Solution
 
tovvenkiCommented:
Hi,
Use the DataKeyField property of the datagrid, set this field to the filename and in the OnDeleteCommand use this property tio get the name of the file and delete it using File.Delete

For e.g
<asp:datagrid id="dgPopularFAQs" runat="server"
          ...
      DataKeyField="FileName">

    ...      
  </asp:datagrid>

Sub articleList_DeleteFile(sender as Object, e as DataGridCommandEventArgs)
    'First, get the filename to delete
    Dim fileName as String = articleList.DataKeys(e.Item.ItemIndex)

    lblMessage.Text = "You opted to delete the file " & _
        fileName & ".<br />" & _
        "This file could be deleted by calling: " & _
        "<code>File.Delete(fileName)</code><p>"
   
    'You would want to rebind the Directory's files to the DataGrid after
    'deleting the file...
  End Sub


I Hope that this helps you, if you need to learn more on using datagrid have a look at this article
http://aspnet.4guysfromrolla.com/articles/090402-1.aspx

regards
Venki
0
 
driven13Author Commented:
Thanx for the response....

All I am looking for is someone to put in the final piece in this routine from above:

  Sub articleList_DeleteFile(sender as Object, e as DataGridCommandEventArgs)
    'First, get the filename to delete
    Dim fileName as String = articleList.DataKeys(e.Item.ItemIndex)

    lblMessage.Text = "You opted to delete the file " & _
        fileName & ".<br />" & _
        "This file could be deleted by calling: " & _
        "<code>File.Delete(fileName)</code><p>"
   
    'You would want to rebind the Directory's files to the DataGrid after
    'deleting the file...
  End Sub

The rest of the code works.  Only the delete button does not.  I have zero expirience using .NET...[:0(

The only thing missing is "to create an event handler for the DataGrid's DeleteCommand event. In this event handler, we need to determine the file to delete and then delete it via the File.Delete() method."

Indebted to you,

--D.
0
 
tovvenkiCommented:
Hi,
this will work
Sub articleList_DeleteFile(sender as Object, e as DataGridCommandEventArgs)
    'First, get the filename to delete
    Dim fileName as String = articleList.DataKeys(e.Item.ItemIndex)

    lblMessage.Text = "You opted to delete the file " & _
        fileName & ".<br />" & _
        "This file could be deleted by calling: " & _
        "<code>File.Delete(fileName)</code><p>"
    File.Delete(filename)
    'You would want to rebind the Directory's files to the DataGrid after
    'deleting the file...
  End Sub

but before deleting a file make sure that the aspnet user has permission to delete the file. If not you will get a permission error.
In order to give aspnet user the permission right click the folder containing the files if you want to give rights on the full folder or if you need to give rights to just delete a file then right on the file, select properties. Select the Security tab and select the aspnet user and give him the necessary permission(read/write).

Hope that this helps you

regards
venki
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
driven13Author Commented:
Yup that works for deleteing the file ( Thank you.) but the same file shows up on the page even though it is gone from the server.

In the routine above how would I "rebind the Directory's files to the DataGrid after deleting the file"...???

Thanx again,

-D.
0
 
tovvenkiCommented:
Hi,
just call the same binding calls that you called in the page_load event handler like

Sub articleList_DeleteFile(sender as Object, e as DataGridCommandEventArgs)
    'First, get the filename to delete
    Dim fileName as String = articleList.DataKeys(e.Item.ItemIndex)

    lblMessage.Text = "You opted to delete the file " & _
        fileName & ".<br />" & _
        "This file could be deleted by calling: " & _
        "<code>File.Delete(fileName)</code><p>"
    File.Delete(filename)
    'You would want to rebind the Directory's files to the DataGrid after
    'deleting the file..

      Dim dirInfo as New DirectoryInfo(Server.MapPath(""))
   
      articleList.DataSource = dirInfo.GetFiles("*.txt")
      articleList.DataBind()
  End Sub

regards
venki
0
 
driven13Author Commented:
Thanx a million for helping me out, venki.

It all works now.

Regards,

--D.
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now