Click here to Skip to main content
15,881,588 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How to list out all the images in the folder in a list view control. The image selected in the list view is to be shown on the image control .

1 solution


Please have a look on following example.

<script type="text/javascript">
        function showimage(fsImage)
            document.getElementById('<%= imgSample.ClientID %>').src = "/images/" + fsImage.text;
            return false;

<form id="form1" runat="server">
            <asp:ListView ID="lvImage" runat="server">
                    <table runat="server" id="table1" >
                      <tr runat="server" id="itemPlaceholder" ></tr>
                    <tr runat="server">
                      <td runat="server">
                        <asp:HyperLink ID="hnkImage" runat="server" onclick='<%# "return showimage(this);"%>'
                            Text='<%# DataBinder.Eval(Container.DataItem,"Text") %>'
        <div id="divSelectedImage">
            <asp:Image ID="imgSample" runat="server" />

Code Behind:
private void fillImage()
    String searchFolder = Server.MapPath("/images");
    String[] filters = new String[] { "jpg", "jpeg", "gif", "png" };
    String[] lsFiles = GetFilesFrom(searchFolder, filters, false);

    List<ListItem> files = new List<ListItem>();
    foreach (string filePath in lsFiles)
        files.Add(new ListItem(Path.GetFileName(filePath), filePath));

    lvImage.DataSource = files;

private String[] GetFilesFrom(String searchFolder, String[] filters, bool isRecursive)
    DirectoryInfo loDirectory = new DirectoryInfo(searchFolder);
    List<String> filesFound = new List<String>();
    var searchOption = isRecursive ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly;
    foreach (var filter in filters)
        filesFound.AddRange(Directory.GetFiles(searchFolder, String.Format("*.{0}", filter), searchOption));
    return filesFound.ToArray();

protected void Page_Load(object sender, EventArgs e)

you will get image('image name') list in listview and on click on image will place that image in image control in below div.

Hope this helps you...!
Share this answer
sandy.L 25-Aug-15 5:11am    
I am not aware of I need this solution in windows store applications

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

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900