Link to home
Start Free TrialLog in
Avatar of driven13
driven13

asked on

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>
 
Avatar of tovvenki
tovvenki

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
Avatar of driven13

ASKER

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.
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
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.
ASKER CERTIFIED SOLUTION
Avatar of tovvenki
tovvenki

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanx a million for helping me out, venki.

It all works now.

Regards,

--D.