0

Here is a script for displaying name of files (for download) in data grid table. Is it possible that to show the latest created file in top row?. Please help.

<%@ 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("D:\SeverUpload\Safety")

        articleList.DataSource = dirInfo.GetFiles("*.*")
  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>
Zo Has
  • 12,599
  • 22
  • 87
  • 149
user1235981
  • 21
  • 2
  • 7
  • Check out this [similar question](http://stackoverflow.com/questions/52842/sorting-directory-getfiles) whose winning answer explains the use of System.IO.DirectoryInfo.GetFileSystemInfos() – DOK Mar 12 '12 at 12:46

2 Answers2

0

Where is the data being loaded in at? It's possibly to just add a ORDER BY DATETIME DESC when querying the database.

jammer
  • 23
  • 8
0

Please use the below in the page load.

If Not Page.IsPostBack then
        Dim dirInfo As New DirectoryInfo("D:\SeverUpload\Safety")
        Dim files As FileSystemInfo() = dirInfo.GetFileSystemInfos("*.*")
        Dim orderedFiles = files.OrderBy(Function(f) f.CreationTime)

        articleList.DataSource = orderedFiles
  articleList.DataBind()
End If
PraveenVenu
  • 8,217
  • 4
  • 30
  • 39