Cấp bậc tác giả:

DOTNET

Hiển thị danh sách tập tin trong thư mục hiện hành

Được viết bởi webmaster ngày 18/10/2014 lúc 08:45 PM
Để có thể hiển thị được danh sách tên các tập tin, cần phải kiểm tra thư mục đã tồn tại hay chưa, nếu đã tồn tại thì cho mảng chứa danh sách các tập tin trong thư mục hiện hành. Rồi chứa trong List. Add vào trong control GridView để hiển thị ngoài website
  • 0
  • 7625

Hiển thị danh sách tập tin trong thư mục hiện hành

 
Để có thể hiển thị được danh sách tên các tập tin, cần phải kiểm tra thư mục đã tồn tại hay chưa, nếu đã tồn tại thì cho mảng chứa danh sách các tập tin trong thư mục hiện hành. Rồi chứa trong List. Add vào trong control GridView để hiển thị ngoài website
Bước 1:
<asp:GridView ID="gvFiles" CellPadding="5" runat="server" AutoGenerateColumns="False"
BackColor="White" BorderColor="#999999" BorderStyle="None" BorderWidth="1px"
GridLines="Vertical" Width="100%">
<AlternatingRowStyle BackColor="#DCDCDC" />
<Columns>
<asp:TemplateField ItemStyle-HorizontalAlign="Center" HeaderStyle-HorizontalAlign="Center"
HeaderText="STT" ItemStyle-Width="5%">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Container.DataItemIndex + 1 %>'></asp:Label>
</ItemTemplate>
<ItemStyle HorizontalAlign="Center" Width="5%" />
</asp:TemplateField>
<asp:TemplateField ItemStyle-Horizontal HeaderStyle-HorizontalAlign="Center"
HeaderText="Tên tập tin" ItemStyle-Width="70%">
<ItemTemplate>
<asp:HyperLink ID="linkTai" ToolTip="Click để tải tập tin về máy" runat="server"
NavigateUrl='<%# "~/../Upload/TepTin/" + Eval("Text") %>'><%# Eval("Text") %></asp:HyperLink>
</ItemTemplate>
<ItemStyle Horizontal Width="70%" />
</asp:TemplateField>
</Columns>
<FooterStyle BackColor="#CCCCCC" ForeColor="Black" />
<HeaderStyle BackColor="Green" Font-Bold="true" ForeColor="White" />
<PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />
<RowStyle BackColor="#EEEEEE" ForeColor="Black" />
<SelectedRowStyle BackColor="#008A8C" Font-Bold="True" ForeColor="White" />
</asp:GridView>
Bước 2:
protected void BindGridview()
{
string sBaoGiaPath = Server.MapPath(Common.sPathUpFile);
if (Directory.Exists(sBaoGiaPath))
{
string[] filesLoc = Directory.GetFiles(Server.MapPath("~/../Upload/TepTin"));
List<ListItem> files = new List<ListItem>();
foreach (string file in filesLoc)
{
//if (Path.GetFileName(file).IndexOf(masv) >= 0)
files.Add(new ListItem(Path.GetFileName(file)));
}
gvFiles.DataSource = files;
gvFiles.DataBind();
}
else
gvFiles.Caption = "Không có tập tin";
}

Nguồn bài viết: DOTNET.VN

BÌNH LUẬN BÀI VIẾT

Bài viết mới nhất

LIKE BOX

Bài viết được xem nhiều nhất

HỌC HTML