当前位置:编程学习 > C#/ASP.NET >>

关于ListBox中后台读取鼠标选中列表文件的问题。

我贴出了代码帮我看看   

就是我用listbox  读取了D盘目录下的文件夹里的文件。然后现在我要通过鼠标选择其中的某一个文件。点击删除按钮。就可以删除文件。但是 每次都报错  我用的是BoxList1.SelectedItem.Value 但是好像每次都获取不了我选中的内容而且报错。
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.IO;

public partial class Admin_ShanchuShuju : System.Web.UI.Page
{
   
    protected void Page_Load(object sender, EventArgs e)
    {
        
        FindFile("D:/beifen/");
    }

    protected void ListBox1_SelectedIndexChanged(object sender, EventArgs e)
    {
       
    }

    public void FindFile(string dirPath) //参数dirPath为指定的目录
     { 
         //在指定目录及子目录下查找文件,在listBox1中列出子目录及文件
           DirectoryInfo Dir=new DirectoryInfo(dirPath);
          try
        {
          foreach(DirectoryInfo d in Dir.GetDirectories())//查找子目录 
            {
              FindFile(Dir+d.ToString()+"");
              ListBox1.Items.Add(Dir+d.ToString()+""); //listBox1中填加目录名
             }
          foreach(FileInfo f in Dir.GetFiles("*.bak")) //查找文件
            {
             ListBox1.Items.Add(Dir+f.ToString()); //listBox1中填加文件名
           
            }
         }
        catch(Exception ex)
        {
           Response.Write(ex.Message);
         }
      }

    protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
    {
        
     
         //this.ListBox1.Items.RemoveAt(this.ListBox1.SelectedIndex);
        string paths = this.ListBox1.SelectedItem.Value;
        FileInfo file = new FileInfo(paths); 
            file.Delete();
            Response.Redirect(Request.Url.ToString());
       
    }
}
   

ListBox 异常 ASP.NET  --------------------编程问答-------------------- 把this去掉看看 --------------------编程问答--------------------     protected void Page_Load(object sender, EventArgs e)    
 {      
if (!IsPostBack)
{             
FindFile("D:/beifen/");    

补充:.NET技术 ,  ASP.NET
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,