Allowing the User to Delete a File

This demo illustrates how to add functionality to the DataGrid to allow the user to delete a file by clicking a "Delete" button next to the file's name. (Note that for this demo, clicking the Delete button does not actually Delete the file... it simply emits a message explaining what file you opted to delete...)


 File NameLast Write TimeFile Size
ArraySortingDemo.aspx11/27/20202,420 bytes
BinarySearch.complex.aspx11/27/20205,392 bytes
BinarySearch.simple.aspx11/27/20204,368 bytes
BindArrayDG.aspx11/27/20201,579 bytes
BindArrayDL.aspx11/27/20201,744 bytes
BindCustomClass.aspx11/27/20203,513 bytes
BindingDataGrid.aspx11/27/20203,504 bytes
blogTest.aspx8/26/20204,206 bytes
BrowserDetection.aspx11/27/20204,252 bytes
ButtonClientSide.aspx11/27/20201,596 bytes
CachedPaging.aspx11/27/20207,744 bytes
Calendar.aspx7/23/20201,583 bytes
Cal_1.aspx11/27/20201,292 bytes
Cal_2.aspx11/27/20204,517 bytes
Cal_3.aspx11/27/20207,325 bytes
CheckboxListDemo.aspx11/27/20207,555 bytes
CollapsibleRepeater.aspx11/27/20205,377 bytes
ConsumeRSS.aspx11/27/20204,869 bytes
csharpexample.aspx11/27/20203,250 bytes
customizeTemplate.aspx11/27/20204,457 bytes
CustPaging.aspx11/27/20205,901 bytes
DataTableCompute.aspx11/27/20204,026 bytes
DataTableExpression.aspx11/27/20202,714 bytes
ddlSelectedItemStyle.aspx11/27/20201,171 bytes
dgExample1.aspx11/27/20202,098 bytes
dgExample10.aspx11/27/20203,542 bytes
dgExample11.aspx11/27/20206,709 bytes
dgExample12.aspx11/27/20205,063 bytes
dgExample13.aspx11/27/20204,160 bytes
dgExample14.aspx11/27/20205,581 bytes
dgExample15.aspx11/27/20207,493 bytes
dgExample16.aspx11/27/20208,167 bytes
dgExample17.aspx11/27/202010,901 bytes
dgExample18.aspx11/27/20205,420 bytes
dgExample19.aspx11/27/20205,728 bytes
dgExample2.aspx11/27/20202,489 bytes
dgExample20.aspx11/27/20207,415 bytes
dgExample21.aspx11/27/20207,652 bytes
dgExample22.aspx11/27/20207,929 bytes
dgExample23.aspx11/27/20205,648 bytes
dgExample24.aspx11/27/20205,213 bytes
dgExample25.aspx11/27/20204,321 bytes
dgExample26.aspx7/23/20204,970 bytes
dgExample27.aspx7/23/20207,201 bytes
dgExample28.aspx7/23/20205,059 bytes
dgExample29.aspx7/23/20203,590 bytes
dgExample3.aspx11/27/20202,894 bytes
dgExample30.aspx11/27/20202,911 bytes
dgExample31.aspx11/27/20203,567 bytes
dgExample32.aspx11/27/20203,868 bytes
dgExample33.aspx11/27/20203,723 bytes
dgExample34.aspx11/27/20205,359 bytes
dgExample35.aspx11/27/20206,629 bytes
dgExample36.aspx11/27/20207,825 bytes
dgExample37.aspx11/27/20209,282 bytes
dgExample4.aspx11/27/20204,220 bytes
dgExample5.aspx11/27/20203,699 bytes
dgExample6.aspx11/27/20204,447 bytes
dgExample7.aspx11/27/20205,560 bytes
dgExample8.aspx11/27/20207,971 bytes
dgExample9.aspx11/27/20205,458 bytes
dgHighlight.aspx11/27/20208,367 bytes
dgRandomOrder.aspx7/23/20203,499 bytes
dgRandomOrder2.aspx7/23/20204,929 bytes
dgRollup.aspx11/27/202010,777 bytes
dgSubHeaders.aspx11/27/20207,481 bytes
dgSummaryDemo.aspx11/27/20202,536 bytes
DHTMLDataGrid.aspx7/23/20201,126 bytes
DispImagesFromDir.aspx11/27/20202,539 bytes
DispImagesFromDirWithResize.aspx11/27/20204,604 bytes
DispImagesFromDirWithResize2.aspx11/27/20205,752 bytes
DisplayXMLInASPNET.aspx7/23/20201,048 bytes
dlExampleRepeatColumns.aspx11/27/20203,268 bytes
DynamicDataTable.aspx11/27/20202,351 bytes
dynCtrls1.aspx11/27/20201,150 bytes
dynCtrls2.aspx11/27/20201,966 bytes
dynCtrls3.aspx11/27/20201,744 bytes
dynCtrls4.aspx11/27/20202,537 bytes
dynCtrls5.aspx11/27/20203,737 bytes
EmailControl.aspx7/23/20203,174 bytes
EmailControl2.aspx7/23/20203,573 bytes
EmailControl3.aspx7/23/20202,829 bytes
enc.aspx11/27/20205,491 bytes
ExpiringDemoA.aspx11/27/20206,112 bytes
ExpiringDemoB.aspx11/27/20207,059 bytes
FamilyTree-TreeView.aspx11/27/20203,748 bytes
FileDeptDataCaching.aspx7/23/20204,601 bytes
filesystemdisp.aspx8/26/2020976 bytes
FormatXML.aspx7/23/2020610 bytes
FreeTextBoxDemo1.aspx11/27/20202,509 bytes
FreeTextBoxDemo2.aspx11/27/20202,854 bytes
GeneratePassword.aspx11/27/20208,860 bytes
GetRenderedHTML.aspx7/23/20204,765 bytes
GoogleCache.aspx8/26/2020600 bytes
GoogleSearch.aspx11/27/20202,416 bytes
GoogleSearch.Full.aspx11/27/20206,122 bytes
hangman.aspx11/27/202014,938 bytes
HoverCalInPage.aspx7/23/20201,991 bytes
HttpWebRequestDemo1.aspx11/27/20202,683 bytes
HyperlinkInNewWindow.aspx8/11/20204,084 bytes
HyperlinkInNewWindowWithJavaScript.aspx8/11/20204,144 bytes
HyperlinkTest.aspx7/23/20204,475 bytes
IsPrime.aspx11/27/20204,810 bytes
IsPrime.Client.aspx11/27/20206,036 bytes
ListArticles.AllowDelete.aspx11/27/20205,990 bytes
ListArticles.aspx11/27/20202,560 bytes
ListArticles.Sortable.aspx11/27/20206,360 bytes
ListBoxBinding.aspx11/27/20203,405 bytes
listboxEditDG.aspx11/27/20207,763 bytes
MasterDetail.aspx11/27/20207,202 bytes
md5demo.aspx7/23/20204,165 bytes
NoncollapsibleRepeater.aspx11/27/20203,225 bytes
oneDDLInFooter.aspx11/27/202011,901 bytes
OutputCaching.aspx11/27/2020914 bytes
PagingDataGrid.aspx11/27/20206,443 bytes
PieChart.aspx7/23/20203,309 bytes
PrettyCorners.aspx7/23/20204,104 bytes
PrettyRSSDisplay.aspx11/27/20203,635 bytes
printPage.aspx11/27/20205,269 bytes
printPage_old.aspx11/27/20202,433 bytes
ProblemNotReturningDisabledControls.aspx8/11/20208,703 bytes
rc4decrypt.aspx11/27/20202,970 bytes
rc4encrypt.aspx11/27/20203,409 bytes
ReadFile.aspx11/27/20202,556 bytes
ReadTextFile.aspx11/27/20202,556 bytes
ReceivingPage.aspx11/27/20201,698 bytes
regex.aspx11/27/20208,438 bytes
RegExPCase.aspx11/27/20203,136 bytes
RFVforCB.aspx11/27/20202,634 bytes
RolloverButtonDemo.aspx7/23/20202,374 bytes
RowHighlighter.aspx11/27/20208,405 bytes
RowSelectorDemo.aspx11/27/20202,959 bytes
rsdg.Demo1.aspx11/27/20204,589 bytes
rsdg.Demo2.aspx11/27/20203,860 bytes
rsdg.Demo3.aspx11/27/20203,895 bytes
rsdg.Demo4.aspx11/27/20203,861 bytes
RssFeed.aspx8/11/20203,728 bytes
RssFeed.code.aspx11/27/20203,943 bytes
RssFeedDemo.aspx11/27/20201,454 bytes
RssFeedDemo.stylin.aspx11/27/20202,115 bytes
ScreenScrapeDemo.aspx11/27/20201,845 bytes
ScrollableDataGrid.aspx7/23/20203,976 bytes
ScrollableDataGridFixedHeader.aspx7/23/20205,219 bytes
SendEmail.aspx11/27/20205,904 bytes
SendingPage.aspx11/27/20203,333 bytes
sf.aspx7/23/2020793 bytes
ShowImage.aspx7/23/2020893 bytes
ShowThumbNail.aspx7/23/20201,661 bytes
simp.cal.aspx7/23/2020724 bytes
simpCal.aspx11/27/2020641 bytes
SimpleBoundColumn.aspx11/27/20203,078 bytes
SimpleRSSDisplay.aspx11/27/20202,028 bytes
skmMenu.aspx11/27/20201,647 bytes
skmMenu.horiz.aspx11/27/20201,999 bytes
skmMenu.stylin.aspx11/27/20201,829 bytes
StaticDataTable.aspx11/27/20202,882 bytes
StripSpaceComparison.aspx11/27/20203,050 bytes
StripWhitespace.aspx11/27/2020772 bytes
TabInterface.aspx11/27/202012,295 bytes
TamperProofURLs.A.aspx11/27/20205,284 bytes
TamperProofURLs.B.aspx11/27/20206,823 bytes
TestRepeater.aspx11/27/202010,802 bytes
thu.aspx7/23/20201,699 bytes
thumb.aspx11/27/202016,962 bytes
ToolbarDemo.aspx11/27/20201,394 bytes
TracingDemo.aspx11/27/20208,925 bytes
TreeViewBinding1.aspx11/27/20201,168 bytes
TreeViewXMLDemo.aspx11/27/20201,498 bytes
treeview_bound.aspx11/27/2020462 bytes
twoDDLsInFooter.aspx11/27/202015,425 bytes
ulRepeaterDemo.aspx11/27/20202,798 bytes
UserAgent.aspx11/27/2020960 bytes
UserInfoWriter.2.aspx7/23/20201,326 bytes
UserInfoWriter.aspx7/23/20203,393 bytes
UsingLimitColumn.aspx11/27/20203,724 bytes
UsingTemplateColumn.aspx11/27/20205,341 bytes
WriteTextFile.aspx11/27/20203,155 bytes
xmlDG.aspx11/27/202012,168 bytes
xmlRead.aspx11/27/20203,803 bytes
xmlRead2.aspx11/27/20203,803 bytes
xmlser.aspx11/27/20208,133 bytes
xmlWrite.aspx11/27/20202,869 bytes
XPathDemo.aspx7/23/2020987 bytes
XQueryDemo.aspx11/27/20202,484 bytes


Source Code
<%@ 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>
  

Return to the article