如题,文件夹内有不同类型文件,需要把一个文件夹内的图片文件在 DataList 里输出
原先是遍历文件夹内所有文件,代码如下,可是只需要输出图片文件DirectoryInfo dir = new DirectoryInfo(Server.MapPath("/UPFile/news/" + txtid.Text));
        FileSystemInfo[] dis = dir.GetFileSystemInfos();
        if (dis.Length >= 1)
        {
            DataList1.DataSource = dis;
            DataList1.DataBind();
        }后来想了个办法过滤类型输出这些图片,可是因为需要添加删除图片按钮我希望能把这些写进 DataSource  去绑定 DataList  int h=1;
        int j = 0;
        txtgndq.Text = "";
        DirectoryInfo dir = new DirectoryInfo(Server.MapPath("/images/icon"));
        foreach (FileInfo dChild in dir.GetFiles("*"))
        {            int i = dChild.Name.LastIndexOf(".");
            string Fname = dChild.Name.Substring(0, i);
            string newext = dChild.Name.Substring(i);   //获取文件扩展名     
            if (newext == ".gif" || newext == ".jpg" || newext == ".jpeg" || newext == ".bmp" || newext == ".png")
            {
                txtgndq.Text += "<img src='/images/icon/" + dChild.Name + "'/> ";
                if ((h % 13) == 0) txtgndq.Text += "<br>";
                h++;
            }
        }求高手指点

解决方案 »

  1.   

    你用模板列数据源把文件名放进去不要写这种东西txtgndq.Text += "<img src='/images/icon/" + dChild.Name + "'/> ";
      

  2.   

    你可以换种做法。可以转化为List<FileSystemInfo>,然后进行Remove 操作。。
    以下是代码:     int h = 1;
                int j = 0;
                txtgndq.Text = "";
                DirectoryInfo dir = new DirectoryInfo(Server.MapPath("/images"));
                List<FileSystemInfo> list = dir.GetFileSystemInfos().ToList<FileSystemInfo>();
                foreach (FileInfo dChild in dir.GetFiles("*"))
                {                int i = dChild.Name.LastIndexOf(".");
                    string Fname = dChild.Name.Substring(0, i);
                    string newext = dChild.Name.Substring(i);   //获取文件扩展名     
                    if (newext == ".gif" || newext == ".jpg" || newext == ".jpeg" || newext == ".bmp" || newext == ".png")
                    {
                        txtgndq.Text += "<img src='/images/" + dChild.Name + "'/> ";
                        if ((h % 13) == 0) txtgndq.Text += "<br>";
                        h++;                }
                    else
                    {
                        list.Remove (list.Find(p=>p.Name==dChild.Name));
                    }
                    
                }
                DataList1.DataSource = list;
                DataList1.DataBind();
      

  3.   

     DirectoryInfo imagesfile = new DirectoryInfo(Server.MapPath("./images"));
            DataList1.DataSource = imagesfile.GetFiles("*.jpg");
            DataList1.DataBind();
    或定义实体类,通过遍历文件夹添加对象到
    List<T> lst=new List<T>();
    再绑定到DataList1
      

  4.   

    用模板,一个DataList控件,一个相应的datasource控件,对Datalist的模板进行修改
      

  5.   

    有没有办法对DataList1.DataSource 进行添加删除数据
      

  6.   

    DataList1.DataSource是绑定用的 你要添加或删除 可以在前边操作啊
      

  7.   

    datasource只是绑定数据源,怎么在它里面进行增删操作呢?要操作也是在它之前操作完在进行绑定即可。这样既简单,也不会出错。