本文英文原版:
http://aspnet.4guysfromrolla.com/articles/052803-1.2.aspx
用DataGrid展示目录下的文件 (第二部分)
在第一部分,我们创建了一个DataGrid控件以显示某个目录下的文件,在本节,我们看如何扩展示例,允许用户仅仅点击一个按钮来删除某个文件。
为DataGrid控件添加Delete按钮
假设我们优化前面的那个示例(示例页面为:http://aspnet.4guysfromrolla.com/demos/ListArticles.aspx),允许用户删除目录下的某个文件.为此,我们可以在DataGrid控件里添加一个名为"Delete" 的ButtonColumn ,添加后,DataGrid控件的声明代码开起来应该和下面的差不多:
<form runat="server">
<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"
<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>
仔细查看上面的代码示例,我们注意到如下3方面:
1.为DataGrid添加了一个ButtonColumn,显示为一个"Delete"按钮.
2.该ButtonColumn的CommandName属性被设置为Delete,那意味着当点击该按钮时触发DataGrid控件的DeleteCommand事件.
3.该DataGrid被放置在Web form里(the <form runat="server">...</form>),这是必要的,因为DataGrid现在包含一个ButtonColumn.
现在我们将为DataGrid的DeleteCommand事件创建一个事件处理器。当DataGrid的某个行记录的 "Delete" 按钮点击时,就触发该事件处理器。所以该处理器先要确定待删除文件的路径,再将其删除掉。
为了确定用户将要删除的文件,我们把DataGrid的DataKeyField属性设置为FileInfo class类的FullName属性.这就意味着,我们可以确定待删除文件的完整路径.代码如下:
<form runat="server">
<asp:DataGrid runat="server" id="articleList"
...
DataKeyField="FullName">
<Columns>
...
</Columns>
</asp:DataGrid>
</form>
剩下要做的便是为DataGrid的DeleteCommand事件创建一个事件处理器.在该处理器里我们需要确定要删除的文件,再通过File.Delete()方法将其删掉.最终的示例代码如下.除了删除文件以外,示例代码还包含一个客户端确认对话框,当用户点击"Delete"按钮后,提醒用户是否真的要删除文件.(关于这种客户端事件的更多讨论见文章《 An Extensive Examination of the DataGrid Web Control: Part 8! 》)
<%@ 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>
结语:
本文我们考察了如何使用NET Framework的与文件系统相关的类,以及如何用DataGrid控件展示指定目录下的文件.另外,我们考察了如何优化DataGrid包含一列"Delete" 按钮,供用户删除某个文件.只需要最少的代码和最短的程序调试时间便可达到上述目的.
祝编程快乐!