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.MapPa th(""))
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("o nclick") = "javascript:return " & _
"confirm('Are you sure you want to delete the file " & _
DataBinder.Eval(e.Item.Dat aItem, "Name") & "?')"
End If
End Sub
Sub articleList_DeleteFile(sen der as Object, e as DataGridCommandEventArgs)
'First, get the filename to delete
Dim fileName as String = articleList.DataKeys(e.Ite m.ItemInde x)
lblMessage.Text = "You opted to delete the file " & _
fileName & ".<br />" & _
"This file could be deleted by calling: " & _
"<code>File.Delete(fileNam e)</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-BackC olor="#eee eee"
HeaderStyle-BackColor="Nav y" HeaderStyle-ForeColor="Whi te"
HeaderStyle-Font-Size="15p t" HeaderStyle-Font-Bold="Tru e"
DataKeyField="FullName"
OnItemDataBound="articleLi st_ItemDat aBound"
OnDeleteCommand="articleLi st_DeleteF ile">
<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>
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.MapPa
articleList.DataSource = dirInfo.GetFiles("*.aspx")
articleList.DataBind()
End If
End Sub
Sub articleList_ItemDataBound(
' 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("o
"confirm('Are you sure you want to delete the file " & _
DataBinder.Eval(e.Item.Dat
End If
End Sub
Sub articleList_DeleteFile(sen
'First, get the filename to delete
Dim fileName as String = articleList.DataKeys(e.Ite
lblMessage.Text = "You opted to delete the file " & _
fileName & ".<br />" & _
"This file could be deleted by calling: " & _
"<code>File.Delete(fileNam
'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
HeaderStyle-BackColor="Nav
HeaderStyle-Font-Size="15p
DataKeyField="FullName"
OnItemDataBound="articleLi
OnDeleteCommand="articleLi
<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=
DataFormatString="{0:d}" />
<asp:BoundColumn DataField="Length" HeaderText="File Size"
ItemStyle-HorizontalAlign=
DataFormatString="{0:#,###
</Columns>
</asp:DataGrid>
</form>
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(sen der as Object, e as DataGridCommandEventArgs)
'First, get the filename to delete
Dim fileName as String = articleList.DataKeys(e.Ite m.ItemInde x)
lblMessage.Text = "You opted to delete the file " & _
fileName & ".<br />" & _
"This file could be deleted by calling: " & _
"<code>File.Delete(fileNam e)</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.
All I am looking for is someone to put in the final piece in this routine from above:
Sub articleList_DeleteFile(sen
'First, get the filename to delete
Dim fileName as String = articleList.DataKeys(e.Ite
lblMessage.Text = "You opted to delete the file " & _
fileName & ".<br />" & _
"This file could be deleted by calling: " & _
"<code>File.Delete(fileNam
'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(sen der as Object, e as DataGridCommandEventArgs)
'First, get the filename to delete
Dim fileName as String = articleList.DataKeys(e.Ite m.ItemInde x)
lblMessage.Text = "You opted to delete the file " & _
fileName & ".<br />" & _
"This file could be deleted by calling: " & _
"<code>File.Delete(fileNam e)</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
this will work
Sub articleList_DeleteFile(sen
'First, get the filename to delete
Dim fileName as String = articleList.DataKeys(e.Ite
lblMessage.Text = "You opted to delete the file " & _
fileName & ".<br />" & _
"This file could be deleted by calling: " & _
"<code>File.Delete(fileNam
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
ASKER
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.
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanx a million for helping me out, venki.
It all works now.
Regards,
--D.
It all works now.
Regards,
--D.
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(sen
'First, get the filename to delete
Dim fileName as String = articleList.DataKeys(e.Ite
lblMessage.Text = "You opted to delete the file " & _
fileName & ".<br />" & _
"This file could be deleted by calling: " & _
"<code>File.Delete(fileNam
'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