Click here to Skip to main content
15,883,705 members
Articles / Web Development / ASP.NET
Tip/Trick

Images on Liveview Control

Rate me:
Please Sign up or sign in to vote.
4.00/5 (3 votes)
6 Mar 2014CPOL 10.1K   3   2
Find Images from Sub Directories and Show in List View Control

Introduction

This is simple and very useful. There are few image files in my project directory and I need to find the files from a specific directory and sub directory, input is the directory name and the images to be shown in list view control.

Using the Code

ASP.NET
//
// <%@ Page Language="C#" AutoEventWireup="true" 
CodeFile="FileManager.aspx.cs" Inherits="FileManager" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <table>
        <tr>
            <td>
                <asp:TextBox ID="txtpath" runat="server"></asp:TextBox>
            </td>
            <td>
                <asp:Button ID="btnpath" runat="server" 
                text="Path" OnClick="btnpath_Click"/>
                     </td>
        </tr>
        <tr>
            <td colspan="2">
                <asp:ListView ID="ListView1" runat="server">
                    <LayoutTemplate>
      <ol>
         <asp:PlaceHolder runat="server" ID="itemPlaceholder"></asp:PlaceHolder>
      </ol>
   </LayoutTemplate>
                    <ItemTemplate>
                         <asp:ImageButton ID="ImageButton1" 
                         runat="server" Width="50px" Height="50px"  
                         imageurl='<%#Bind("Name") %>'></asp:ImageButton>
                         
                    </ItemTemplate>
                </asp:ListView>
            </td>
        </tr>
    </table>
    </div>
    </form>
</body>
</html>

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

public partial class FileManager : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
        protected void btnpath_Click(object sender, EventArgs e)
        {
            ArrayList list = new ArrayList();
    
            try
            {
                string searchpath = txtpath.Text;
                DirectoryInfo dir = new DirectoryInfo(Server.MapPath(@"~/"));
                DirectoryInfo[] di = dir.GetDirectories(searchpath,SearchOption.AllDirectories);
                FileInfo[] files = null;
                foreach (DirectoryInfo d in di)
                {
                     files= d.GetFiles("*.*", SearchOption.AllDirectories);
                }
                ArrayList list1 = new ArrayList();
                foreach (FileInfo file2 in files)
                {
                    string a = Server.MapPath(@"~/");
                    string b = file2.FullName.Replace(a, "~/");
                    string c = b.Replace("\\", "/");

                    list1.Add(c);                

                    list.Add(file2);
                }
            
                ListView1.DataSource = list;
                ListView1.DataBind();
                int _i = 0;
                foreach (ListViewItem li in ListView1.Items)
                {
                    Image im = (Image)li.FindControl("ImageButton1");
                    im.ImageUrl = list1[_i].ToString();
                    _i = _i + 1;
                }
            }
            catch(Exception ex)
            {

            }
        }
//

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Team Leader Inland World Logistics (P) Ltd.
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralMy vote of 4 Pin
jgakenhe7-Mar-14 4:02
professionaljgakenhe7-Mar-14 4:02 
GeneralMy vote of 2 Pin
Member 106020826-Mar-14 19:09
Member 106020826-Mar-14 19:09 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.