Getting Dynamic Path based on link that is clicked

//aspx.cs file

  using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Web;
  using System.Web.UI;
  using System.Web.UI.WebControls;
  using System.IO;

  public partial class trash : System.Web.UI.Page
protected void Page_Load(object sender, EventArgs e)
    Response.Write("<table style='width: 10px; height: 10px; margin-left:100px'>");

    foreach(var directory in new DirectoryInfo("C:\\Users\\naresh\\Documents\\Visual  Studio 2010\\WebSites\\CMANAGER").GetDirectories())

 Response.Write( "<tr>");      
    Response.Write("<td><a href= view4.aspx?folder="+ directory.Name + "> "+ directory.Name +"</a></td>");



With this code I'm listing all directories in the given directory with hyperlinks.So,If I click on the hyperlink now I should list all the files in the Particular directory in a separate page.But I'm having problem in giving Dynamic path based on the hyperlink clicked. Please help me in this regard. Thank you..


I think you want to use directory.FullName for the hyperlink. You many need to url encode it and then decode it on the new page when you read it off the query string.

On view4.aspx after reading the folder off the query string, create a directory info object again and iterate over the results of directory.GetFiles()

Here is a link to the DirectoryInfo class for more information

Using ASP.NET MVC 2 (which is much better than putting everything into Page_Load), you could do it like this:


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());


<%@ Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master" 
  Inherits="System.Web.Mvc.ViewPage<System.IO.DirectoryInfo[]>" %>

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
  <table style='width: 10px; height: 10px; margin-left:100px'>
    <% foreach (var directory in Model)
      { %>
            <%= Html.ActionLink(
                    directory.Name, "Index",
                    new RouteValueDictionary { { "path", directory.FullName } }) %>

Need Your Help

A good model class for mvc, keeping track of properties

php database model-view-controller class model

Here is my model_users class (the "model" it extends from doesn't have anything yet - still trying to work on getting this one ready and then abstracting out common functions for use with other mod...

Phonegap InsertID indefined


I'm running into a little trouble and I am hoping some one out there can help.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.