Problem showing files in folder, with pageload but if i use Button1.Click then no pro

siraero

Active member
Joined
Jan 21, 2012
Messages
32
Programming Experience
Beginner
Will not show files in folder, i get a BLANK page.

Hi

Im trying to learn how to upload files to an folder, and then show the files from the folder, I can easy upload files to the folder in VS2010 (on the pc) and on the server (Unoeuro) but when i will see the files in the folder, the page load but i get a Blank page, on my pc and on the server, so its the same (error)

Code
Main page
VB.NET:
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="show_file.aspx.vb" Inherits="show_file" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
<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>
    </div>
    </form>
</body>
</html>

CodeBehind
VB.NET:
Imports System.IO

Partial Class show_file
    Inherits System.Web.UI.Page

    Sub Page_Load(sender As Object, e As EventArgs)
        If Not Page.IsPostBack Then
            Dim dirInfo As New DirectoryInfo(Server.MapPath("~/Upload/"))

            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
End Class

Can someone help me !?
 
I got problems showing files from a folder, with Page_Load.
But if i add a Button Click event, then i got no problem.

I have a folder named "upload" if i use this code with Page_Load i get a blank page...
I have tryed deleting the button and the "Me.TextBox1.Text.Trim &" from the GetFile in Code Behind, but i still get a blank page..

Main Code
VB.NET:
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="showfilespageload.aspx.vb" Inherits="ajax_demo_Demo_showfilespageload" %>  <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server">     <title>Untitled Page</title> </head> <body>     <form id="form1" runat="server">         <div>             <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>             <asp:Button ID="Button1" runat="server" Text="Button" /><br />             <asp:Repeater ID="Repeater1" runat="server">                 <ItemTemplate>                     <asp:HyperLink ID="HyperLink1" runat="server"                              Text='<%# Eval("Name") %>'                              NavigateUrl='<%#  Eval("Name","~/{0}") %>'                              Target="_blank" />                     <br />                 </ItemTemplate>             </asp:Repeater>         </div>     </form> </body> </html>

Code Behind
VB.NET:
Partial Class ajax_demo_Demo_showfilespageload     Inherits System.Web.UI.Page      Sub Page_Load(sender As Object, e As EventArgs)          Dim dir As System.IO.DirectoryInfo         Dim files() As System.IO.FileInfo          'get the root directory of our web app         dir = New System.IO.DirectoryInfo(Server.MapPath("upload/"))          'find all files that start with our user input         files = dir.GetFiles(Me.TextBox1.Text.Trim & "*")          'bind to screen         Me.Repeater1.DataSource = files         Me.Repeater1.DataBind()      End Sub  End Class

But if i use this code in the same root and for the same "upload" folder, then i get the filelist just fine, from the folder, with a Button Click event.

Why cant i use page_load !?

WORKING CODE
Main Code
VB.NET:
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="showfiles.aspx.vb" Inherits="ajax_demo_Demo_showfiles" %>  <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server">     <title>Untitled Page</title> </head> <body>     <form id="form1" runat="server">         <div>             <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>             <asp:Button ID="Button1" runat="server" Text="Button" /><br />             <asp:Repeater ID="Repeater1" runat="server">                 <ItemTemplate>                     <asp:HyperLink ID="HyperLink1" runat="server"                              Text='<%# Eval("Name") %>'                              NavigateUrl='<%#  Eval("Name","~/{0}") %>'                              Target="_blank" />                     <br />                 </ItemTemplate>             </asp:Repeater>         </div>     </form> </body> </html>

Code Behind
VB.NET:
Partial Class ajax_demo_Demo_showfiles     Inherits System.Web.UI.Page      Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click          Dim dir As System.IO.DirectoryInfo         Dim files() As System.IO.FileInfo          'get the root directory of our web app         dir = New System.IO.DirectoryInfo(Server.MapPath("upload/"))          'find all files that start with our user input         files = dir.GetFiles(Me.TextBox1.Text.Trim & "*")          'bind to screen         Me.Repeater1.DataSource = files         Me.Repeater1.DataBind()      End Sub  End Class
 
Back
Top