【问题标题】:Getting Dynamic Path based on link that is clicked根据点击的链接获取动态路径
【发布时间】:2011-05-18 19:32:44
【问题描述】:
//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>");

 Response.Write("</tr>");
        }
    Response.Write("</table>");
}

}

使用此代码,我将使用超链接列出给定目录中的所有目录。因此,如果我现在单击超链接,我应该在单独的页面中列出特定目录中的所有文件。但是我遇到了问题根据点击的超链接给出动态路径。 请在这方面帮助我。 谢谢。。

【问题讨论】:

    标签: c# asp.net gridview directory dynamic-data


    【解决方案1】:

    我认为您想使用 directory.FullName 作为超链接。当您从查询字符串中读取它时,您需要对其进行 url 编码,然后在新页面上对其进行解码。

    在 view4.aspx 上从查询字符串中读取文件夹后,再次创建目录信息对象并遍历 directory.GetFiles() 的结果

    这里是 DirectoryInfo 类的链接以获取更多信息 http://msdn.microsoft.com/en-us/library/system.io.directoryinfo.aspx

    【讨论】:

      【解决方案2】:

      使用 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());
              }
          }
      }
      

      索引.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>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-10-30
        • 2015-03-23
        • 1970-01-01
        • 1970-01-01
        • 2017-11-11
        • 2012-09-29
        • 1970-01-01
        相关资源
        最近更新 更多