We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now

x

Displaying and deleteing files over the web

driven13
driven13 asked
on
Medium Priority
398 Views
Last Modified: 2008-02-01
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>
 
Comment
Watch Question

Commented:
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

Author

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.

Commented:
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

Author

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.
Commented:
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

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

Author

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

It all works now.

Regards,

--D.
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.