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.aspx5/11/20172,410 bytes
BinarySearch.complex.aspx5/11/20175,239 bytes
BinarySearch.simple.aspx5/11/20174,231 bytes
BindArrayDG.aspx5/11/20171,569 bytes
BindArrayDL.aspx5/11/20171,734 bytes
BindCustomClass.aspx5/11/20173,410 bytes
BindingDataGrid.aspx5/11/20173,492 bytes
blogTest.aspx5/11/20174,203 bytes
BrowserDetection.aspx5/11/20174,162 bytes
ButtonClientSide.aspx5/11/20171,546 bytes
CachedPaging.aspx5/11/20177,734 bytes
Calendar.aspx5/11/20171,583 bytes
Cal_1.aspx5/11/20171,282 bytes
Cal_2.aspx5/11/20174,507 bytes
Cal_3.aspx5/11/20177,315 bytes
CheckboxListDemo.aspx5/11/20177,541 bytes
CollapsibleRepeater.aspx5/11/20175,367 bytes
ConsumeRSS.aspx5/11/20174,734 bytes
csharpexample.aspx5/11/20173,239 bytes
customizeTemplate.aspx5/11/20174,447 bytes
CustPaging.aspx5/11/20175,701 bytes
DataTableCompute.aspx5/11/20174,016 bytes
DataTableExpression.aspx5/11/20172,704 bytes
ddlSelectedItemStyle.aspx5/11/20171,161 bytes
dgExample1.aspx5/11/20172,088 bytes
dgExample10.aspx5/11/20173,532 bytes
dgExample11.aspx5/11/20176,699 bytes
dgExample12.aspx5/11/20175,053 bytes
dgExample13.aspx5/11/20174,150 bytes
dgExample14.aspx5/11/20175,571 bytes
dgExample15.aspx5/11/20177,483 bytes
dgExample16.aspx5/11/20178,157 bytes
dgExample17.aspx5/11/201710,891 bytes
dgExample18.aspx5/11/20175,410 bytes
dgExample19.aspx5/11/20175,718 bytes
dgExample2.aspx5/11/20172,479 bytes
dgExample20.aspx5/11/20177,405 bytes
dgExample21.aspx5/11/20177,642 bytes
dgExample22.aspx5/11/20177,919 bytes
dgExample23.aspx5/11/20175,638 bytes
dgExample24.aspx5/11/20175,203 bytes
dgExample25.aspx5/11/20174,311 bytes
dgExample26.aspx5/11/20174,970 bytes
dgExample27.aspx5/11/20177,201 bytes
dgExample28.aspx5/11/20175,059 bytes
dgExample29.aspx5/11/20173,590 bytes
dgExample3.aspx5/11/20172,884 bytes
dgExample30.aspx5/11/20172,901 bytes
dgExample31.aspx5/11/20173,557 bytes
dgExample32.aspx5/11/20173,858 bytes
dgExample33.aspx5/11/20173,713 bytes
dgExample34.aspx5/11/20175,349 bytes
dgExample35.aspx5/11/20176,619 bytes
dgExample36.aspx5/11/20177,815 bytes
dgExample37.aspx5/11/20179,272 bytes
dgExample4.aspx5/11/20174,210 bytes
dgExample5.aspx5/11/20173,689 bytes
dgExample6.aspx5/11/20174,437 bytes
dgExample7.aspx5/11/20175,550 bytes
dgExample8.aspx5/11/20177,961 bytes
dgExample9.aspx5/11/20175,448 bytes
dgHighlight.aspx5/11/20178,357 bytes
dgRandomOrder.aspx5/11/20173,499 bytes
dgRandomOrder2.aspx5/11/20174,929 bytes
dgRollup.aspx5/11/201710,767 bytes
dgSubHeaders.aspx5/11/20177,471 bytes
dgSummaryDemo.aspx5/11/20172,526 bytes
DHTMLDataGrid.aspx5/11/20171,126 bytes
DispImagesFromDir.aspx5/11/20172,529 bytes
DispImagesFromDirWithResize.aspx5/11/20174,594 bytes
DispImagesFromDirWithResize2.aspx5/11/20175,742 bytes
DisplayXMLInASPNET.aspx5/11/20171,048 bytes
dlExampleRepeatColumns.aspx5/11/20173,258 bytes
DynamicDataTable.aspx5/11/20172,340 bytes
dynCtrls1.aspx5/11/20171,140 bytes
dynCtrls2.aspx5/11/20171,956 bytes
dynCtrls3.aspx5/11/20171,734 bytes
dynCtrls4.aspx5/11/20172,527 bytes
dynCtrls5.aspx5/11/20173,727 bytes
EmailControl.aspx5/11/20173,174 bytes
EmailControl2.aspx5/11/20173,573 bytes
EmailControl3.aspx5/11/20172,829 bytes
enc.aspx5/11/20175,478 bytes
ExpiringDemoA.aspx5/11/20176,102 bytes
ExpiringDemoB.aspx5/11/20177,049 bytes
FamilyTree-TreeView.aspx5/11/20173,738 bytes
FileDeptDataCaching.aspx5/11/20174,601 bytes
filesystemdisp.aspx5/11/2017975 bytes
FormatXML.aspx5/11/2017610 bytes
FreeTextBoxDemo1.aspx5/11/20172,441 bytes
FreeTextBoxDemo2.aspx5/11/20172,780 bytes
GeneratePassword.aspx5/11/20178,849 bytes
GetRenderedHTML.aspx5/11/20174,765 bytes
GoogleCache.aspx5/11/2017599 bytes
GoogleSearch.aspx5/11/20172,406 bytes
GoogleSearch.Full.aspx5/11/20176,112 bytes
hangman.aspx5/11/201714,928 bytes
HoverCalInPage.aspx5/11/20171,991 bytes
HttpWebRequestDemo1.aspx5/11/20172,673 bytes
HyperlinkInNewWindow.aspx5/11/20174,084 bytes
HyperlinkInNewWindowWithJavaScript.aspx5/11/20174,144 bytes
HyperlinkTest.aspx5/11/20174,475 bytes
IsPrime.aspx5/11/20174,800 bytes
IsPrime.Client.aspx5/11/20176,026 bytes
ListArticles.AllowDelete.aspx5/11/20175,980 bytes
ListArticles.aspx5/11/20172,550 bytes
ListArticles.Sortable.aspx5/11/20176,350 bytes
ListBoxBinding.aspx5/11/20173,394 bytes
listboxEditDG.aspx5/11/20177,753 bytes
MasterDetail.aspx5/11/20176,995 bytes
md5demo.aspx5/11/20174,165 bytes
NoncollapsibleRepeater.aspx5/11/20173,215 bytes
oneDDLInFooter.aspx5/11/201711,891 bytes
OutputCaching.aspx5/11/2017904 bytes
PagingDataGrid.aspx5/11/20176,432 bytes
PieChart.aspx5/11/20173,309 bytes
PrettyCorners.aspx5/11/20174,104 bytes
PrettyRSSDisplay.aspx5/11/20173,625 bytes
printPage.aspx5/11/20175,257 bytes
printPage_old.aspx5/11/20172,420 bytes
ProblemNotReturningDisabledControls.aspx5/11/20178,703 bytes
rc4decrypt.aspx5/11/20172,960 bytes
rc4encrypt.aspx5/11/20173,399 bytes
ReadFile.aspx5/11/20172,546 bytes
ReadTextFile.aspx5/11/20172,546 bytes
ReceivingPage.aspx5/11/20171,688 bytes
regex.aspx5/11/20178,423 bytes
RegExPCase.aspx5/11/20173,126 bytes
RFVforCB.aspx5/11/20172,623 bytes
RolloverButtonDemo.aspx5/11/20172,374 bytes
RowHighlighter.aspx5/11/20178,393 bytes
RowSelectorDemo.aspx5/11/20172,949 bytes
rsdg.Demo1.aspx5/11/20174,579 bytes
rsdg.Demo2.aspx5/11/20173,850 bytes
rsdg.Demo3.aspx5/11/20173,885 bytes
rsdg.Demo4.aspx5/11/20173,851 bytes
RssFeed.aspx5/11/20173,728 bytes
RssFeed.code.aspx5/11/20173,933 bytes
RssFeedDemo.aspx5/11/20171,444 bytes
RssFeedDemo.stylin.aspx5/11/20172,105 bytes
ScreenScrapeDemo.aspx5/11/20171,834 bytes
ScrollableDataGrid.aspx5/11/20173,976 bytes
ScrollableDataGridFixedHeader.aspx5/11/20175,219 bytes
SendEmail.aspx5/11/20175,893 bytes
SendingPage.aspx5/11/20173,323 bytes
sf.aspx5/11/2017793 bytes
ShowImage.aspx5/11/2017893 bytes
ShowThumbNail.aspx5/11/20171,661 bytes
simp.cal.aspx5/11/2017724 bytes
simpCal.aspx5/11/2017631 bytes
SimpleBoundColumn.aspx5/11/20173,068 bytes
SimpleRSSDisplay.aspx5/11/20172,018 bytes
skmMenu.aspx5/11/20171,637 bytes
skmMenu.horiz.aspx5/11/20171,989 bytes
skmMenu.stylin.aspx5/11/20171,819 bytes
StaticDataTable.aspx5/11/20172,872 bytes
StripSpaceComparison.aspx5/11/20173,040 bytes
StripWhitespace.aspx5/11/2017762 bytes
TabInterface.aspx5/11/201712,285 bytes
TamperProofURLs.A.aspx5/11/20175,274 bytes
TamperProofURLs.B.aspx5/11/20176,812 bytes
TestRepeater.aspx5/11/201710,792 bytes
thu.aspx5/11/20171,699 bytes
thumb.aspx5/11/201716,952 bytes
ToolbarDemo.aspx5/11/20171,384 bytes
TracingDemo.aspx5/11/20178,911 bytes
TreeViewBinding1.aspx5/11/20171,158 bytes
TreeViewXMLDemo.aspx5/11/20171,488 bytes
treeview_bound.aspx5/11/2017452 bytes
twoDDLsInFooter.aspx5/11/201715,415 bytes
ulRepeaterDemo.aspx5/11/20172,788 bytes
UserAgent.aspx5/11/2017950 bytes
UserInfoWriter.2.aspx5/11/20171,326 bytes
UserInfoWriter.aspx5/11/20173,393 bytes
UsingLimitColumn.aspx5/11/20173,714 bytes
UsingTemplateColumn.aspx5/11/20175,331 bytes
WriteTextFile.aspx5/11/20173,145 bytes
xmlDG.aspx5/11/201712,158 bytes
xmlRead.aspx5/11/20173,793 bytes
xmlRead2.aspx5/11/20173,793 bytes
xmlser.aspx5/11/20178,122 bytes
xmlWrite.aspx5/11/20172,859 bytes
XPathDemo.aspx5/11/2017987 bytes
XQueryDemo.aspx5/11/20172,474 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