Используя ASP.NET MVC 2 (что намного лучше, чем помещать все в Page_Load
), вы можете сделать это так:
HomeController.cs:
using System.IO;
using System.Web.Mvc;
namespace SO_web_directory.Controllers
{
public class HomeController : Controller
{
private static readonly string DefaultDirectory = @"C:\";
public ActionResult Index(string path)
{
if (string.IsNullOrWhiteSpace(path))
path = DefaultDirectory;
return View(new DirectoryInfo(path).GetDirectories());
}
}
}
index.aspx:
<%@ Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master"
Inherits="System.Web.Mvc.ViewPage<System.IO.DirectoryInfo[]>" %>
<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
Directories
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<table style='width: 10px; height: 10px; margin-left:100px'>
<% foreach (var directory in Model)
{ %>
<tr>
<td>
<%= Html.ActionLink(
directory.Name, "Index",
new RouteValueDictionary { { "path", directory.FullName } }) %>
</td>
</tr>
<%
}%>
</table>
</asp:Content>