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" />
<aspataGrid 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>
</aspataGrid>
</form>
</body>
</html>
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" />
<aspataGrid 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>
</aspataGrid>
</form>
</body>
</html>