Click here to Skip to main content
15,879,348 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 .
Posted

1 solution

Hi,

Please have a look on following example.

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

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



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;
    lvImage.DataBind();
}

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)
{
    if(!IsPostBack)
    {
        fillImage();
    }
}


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
 
Comments
sandy.L 25-Aug-15 5:11am    
I am not aware of asp.net. 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