单击超链接时删除目录 [英] Deleting a directory when clicked on a hyperlink

查看:125
本文介绍了单击超链接时删除目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在单击超链接时删除目录.我尝试如下操作.但是我的页面重定向到 default(start)页面,并且目录不会删除.

I want to delete a directory when its clicked on a hyperlink.I tried like the below.But my page redirecting to default(start) page and the directory is not deleting.

protected void Page_Load(object sender, EventArgs e)
{
    Execute(s,Content,k,j);
}

private void Execute(string path,string cont,string sym,string space )
{
    foreach (var directory in new DirectoryInfo(path).GetDirectories())
    {
        string f = directory.FullName;  
        f = Server.UrlPathEncode(f);

        Response.Write("<a href =''" + "onclick='Delete(" + f + ")'> DELETE </a>");

        Execute(directory.FullName,cont1,sym1,space1);
    }
}

private void Delete(string path)
{
    DirectoryInfo DirDel = new DirectoryInfo(path);
    DirDel.Delete();
}

您能告诉我这段代码中的问题吗?

Can you tell me the problem in this code?

推荐答案

一般性问题是您创建客户端URL,但是调用方法是服务器端.因此,您需要创建服务器端URL,并处理回发单击:

The general problem is that you create client-side URLs, but method to call is server-side. So you need to create server-side URLs, and handle postback click:

string path = @"d:\Temp";

protected override void OnInit(EventArgs e)
{
    string dir = this.Request["dir"];
    if (String.IsNullOrEmpty(dir)) // write links
    {
        foreach (var di in new DirectoryInfo(path).EnumerateDirectories())
        {
            var link = new HyperLink()
            {
                Text = di.Name,
                NavigateUrl = String.Format("?dir={0}", HttpUtility.UrlEncode(di.Name))
            };
            this.Controls.Add(link);
        }
    }
    else // process link click
    {
        dir = HttpUtility.UrlDecode(dir);
        path = Path.Combine(path, dir);
        Directory.Delete(path);

        Response.Redirect("~/Default.aspx"); // page's name to refresh content
    }
}

这篇关于单击超链接时删除目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆