Need help with file deletion script

G

Guest

Hi,

I have written a webpage that allows a user to delete files in asp.net with
I am having a small problem.

To access this page a user has to login via login.aspx page. After
successful login, user is directed to a page called view.aspx which shows the
user the files in a directory and allows them to delete the files. This page
has a data grid having 4 columns - delete button, File Name, Last Write Time
and File Size. When a user clicks on a delete button to delete say a file
named "test.txt", the file is deleted and data grid is getting refreshed
properly. Immediately after deleting file "test.txt", if I add this file
again to the folder and then if I refresh the view.aspx page then the script
again deletes the "test.txt" file as if I pressed on Delete button.

Can someone help me locate the error? I have pasted the code of view.aspx
below so that you can see what I am doing.

Thanks in advance,

Joe


<%@ Page Language="VB" Debug="true" %>
<%@ Import Namespace="System.IO" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.OLEDB" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HEAD>
<title></title>
<script language="VB" runat="server">
Sub Page_Load(sender as Object, e as EventArgs)
If len(session("memname")) = 0 then
response.Redirect("login.aspx")
Else
Try
Dim folderNameStr As String

folderNameStr = session("memname").toString()
Dim strFilePath As String = "E:\somefolder\" + folderNameStr

If Not Page.IsPostBack then
Dim dirInfo As New DirectoryInfo(strFilePath)

articleList.DataSource = dirInfo.GetFiles("*.*")

If articleList.DataSource.Length = 0 Then
lblMessage.Text = "This account does not contain any files."
Else
articleList.DataBind()
End If

End If
Catch dnfException as DirectoryNotFoundException
lblMessage.Text = "This account does not contain any files."
End Try

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)
'get the filename to delete

Dim fileName as String = articleList.DataKeys(e.Item.ItemIndex)


File.Delete(fileName)

'rebind the Directory's files to the DataGrid after
'deleting the file...

Dim folderNameStr As String

folderNameStr = session("memname").toString()
Dim strFilePath As String = "E:\somefolder\" + folderNameStr
Dim dirInfo As New DirectoryInfo(strFilePath)

articleList.DataSource = dirInfo.GetFiles("*.*")


If articleList.DataSource.Length = 0 Then
articleList.visible = false
lblMessage.Text = "This account does not contain any files."
Else
articleList.visible = True
articleList.DataBind()
End If

End Sub
</script>
<meta http-equiv="Content-Type" content="text/html;">
</HEAD>
<body>

<form runat="server">
<asp:label runat="server" id="lblMessage" />
<asp:DataGrid runat="server" id="articleList"
AutoGenerateColumns="False" DataKeyField="FullName"
OnItemDataBound="articleList_ItemDataBound"
OnDeleteCommand="articleList_DeleteFile">
<Columns>
<asp:ButtonColumn Text="Delete" ButtonType="PushButton" HeaderText=""
CommandName="Delete" HeaderStyle-HorizontalAlign="center"
ItemStyle-HorizontalAlign="center" HeaderStyle-Width="100px" />
<asp:BoundColumn
DataField="Name" HeaderText="File Name" HeaderStyle-HorizontalAlign="center"
/>
<asp:BoundColumn
DataField="LastWriteTime" HeaderText="Last Write Time"
HeaderStyle-HorizontalAlign="center" HeaderStyle-Width="150px"
ItemStyle-HorizontalAlign="Center" DataFormatString="{0:d}" />
<asp:BoundColumn
DataField="Length" HeaderText="File Size"
HeaderStyle-HorizontalAlign="center"
ItemStyle-HorizontalAlign="Right" DataFormatString="{0:#,### bytes}" />
</Columns>
</asp:DataGrid>
</form>
</body>
</html>
 
S

Scott Allen

What you might want to do is compare the timestamp of the file the
user wants to delete versus the timestamp of the file now on disk.
 
H

Hans Kesting

Joe said:
Hi,

I have written a webpage that allows a user to delete files in
asp.net with I am having a small problem.

To access this page a user has to login via login.aspx page. After
successful login, user is directed to a page called view.aspx which
shows the user the files in a directory and allows them to delete the
files. This page has a data grid having 4 columns - delete button,
File Name, Last Write Time and File Size. When a user clicks on a
delete button to delete say a file named "test.txt", the file is
deleted and data grid is getting refreshed properly. Immediately
after deleting file "test.txt", if I add this file again to the
folder and then if I refresh the view.aspx page then the script again
deletes the "test.txt" file as if I pressed on Delete button.

Can someone help me locate the error? I have pasted the code of
view.aspx below so that you can see what I am doing.

Thanks in advance,

Joe

What you can do is, after the delete, *redirect* to the same page.
That way you loose the "postback action", so a refresh just gets
the listing.

Hans Kesting
 
G

Guest

Hans, you are amazing. Yes redirecting the page to itself worked.
Redirecting the page to itself breaks the postback action on a page refersh.
Thank you.

Joe
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,930
Messages
2,570,072
Members
46,521
Latest member
JamieCooch

Latest Threads

Top