By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,866 Members | 1,704 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,866 IT Pros & Developers. It's quick & easy.

Need help with file deletion script

P: n/a
Joe
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>

Nov 19 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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.

--
Scott
http://www.OdeToCode.com/blogs/scott/
On Thu, 19 May 2005 14:02:06 -0700, Joe
<Jo*@discussions.microsoft.com> wrote:
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>


Nov 19 '05 #2

P: n/a
Joe wrote:
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
Nov 19 '05 #3

P: n/a
Joe
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
"Hans Kesting" wrote:
Joe wrote:
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

Nov 19 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.