Click here to Skip to main content
15,390,185 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have an images folder in which i have 3 different. I want to images from those three folder into datalist how can i do that.
At present i am showing images present only in image folder using this code:

<asp:DataList ID="dlImages" runat="server" RepeatColumns="3" CellPadding="3" 
                    style="margin-left: 0px">
<a id="imageLink" href='<%# Eval("ImageName","~/Images/{0}") %>' title='<%#Eval("Description") %>' rel="lightbox[Brussels]"  runat="server" >
<asp:Image ID="Image1" ImageUrl='<%# Bind("ImageName", "~/Images/{0}") %>' runat="server" Width="100" Height="100" />
<ItemStyle BorderColor="Brown" BorderStyle="dotted" BorderWidth="3px" HorizontalAlign="Center" VerticalAlign="Bottom" />

Now i want to show images in that datalist from three different folder how can i do that
Thanks in advance

href='<%# Eval("ImageName","~/Images/{0}") %>'
href='<%# Eval("ImageName","~/{0}") %>'
and store the folder name in the variable you insert for {0}.
amitesh1989 19-Jun-13 4:57am
didn't get your point -Store folder name in the variable
lukeer 19-Jun-13 5:24am
You have here "~/Images/", which supposedly is the image folder. After that, there is a "{0}" which will be replaced with the image file name.
The image file name should be accessible through a variable.

Provide the image path including folder and filename in that variable and substitute the hard coded folder.
amitesh1989 19-Jun-13 5:01am
the think is that first i used to store images in images folder now i create different folder automatically on click event of button and now i want show those images from which present in different folder into my datalist and i have some javascript on it to popup that image and show details of it so there will be x number of folder in images folder
amitesh1989 19-Jun-13 5:54am
suppose if i dont want to hard code that image path because their are x number of folder so how to put path of all the sub-folder of image folder into it. Is their any way to that
Bajirao_ 19-Jun-13 6:48am
I think you can use "ItemDataBound" event.
Get the "ImageName" and search it in current & subfolders.
Then accordingly bind url's for both controls.

<asp:DataList ID="dlImages" runat="server" RepeatColumns="3" CellPadding="3"
Style="margin-left: 0px" ondatabinding="dlImages_DataBinding"

Object objImageLink = e.Item.FindControl("imageLink");
if (objImageLink != null)
HtmlAnchor anchor = objImageLink as HtmlAnchor;
anchor.HRef = filepath;
amitesh1989 19-Jun-13 7:59am
Prasad tell me where should i use object objimage link on item databound or on databinding
Bajirao_ 19-Jun-13 9:04am
use ItemDataBound.
amitesh1989 19-Jun-13 9:22am
can you tell me how i can place the whole code prasad it will be a great help
// Amitesh, This is the working POC.
// As i did my best for you.

// Folders: ~/Image, ~/Image/Pictures, ~/Image/Snaps
// Each one has one image file.

// Mark it as ans, if you success in what you want.

<asp:datalist id="dlImages" runat="server" repeatcolumns="3" cellpadding="3" xmlns:asp="#unknown">
        Style="margin-left: 0px"  
            <a id="imageLink">
                rel="lightbox[Brussels]" runat="server">
                <img id="Image1"  runat="server" />
        <itemstyle bordercolor="Brown" borderstyle="dotted" borderwidth="3px" horizontalalign="Center">
            VerticalAlign="Bottom" />

public partial class _Default : System.Web.UI.Page
        DataTable resultTable;

        protected void Page_Load(object sender, EventArgs e) 
            // Dummy data
            resultTable = new DataTable();


            DataRow dr = resultTable.NewRow();
            dr["ID"] = 1;
            dr["Description"] = "Description 1";
            dr["ImageName"] = "Sharepoint-2010-powered-by-itgroove.jpg";

            dr = resultTable.NewRow();
            dr["ID"] = 2;
            dr["Description"] = "Description 2";
            dr["ImageName"] = "nature_6.jpg";

            dr = resultTable.NewRow();
            dr["ID"] = 3;
            dr["Description"] = "Description 3";
            dr["ImageName"] = "werkzoekende.png";

            dlImages.DataSource = resultTable;


        protected void dlImages_ItemDataBound(object sender, DataListItemEventArgs e)
            string filepath = GetImagePath(Convert.ToString(resultTable.Rows[e.Item.ItemIndex]["ImageName"]));
                // set anchor tag
                Object objImageLink = e.Item.FindControl("imageLink");
                if (objImageLink != null)
                    HtmlAnchor anchor = objImageLink as HtmlAnchor;
                    anchor.HRef = filepath;

                // set image control
                Object objImage = e.Item.FindControl("Image1");
                if (objImage != null)
                    HtmlImage img = objImage as HtmlImage;
                    img.Src = filepath;
                    img.Width = 100;
                    img.Height = 100;
       // To get image path
        public string GetImagePath(string imgName)
            string imgFolderPath = Server.MapPath("~/Images");  
            string[] seprator = {"Images"}; 
            string filepath = string.Empty;

            var file = Directory.GetFiles(imgFolderPath, imgName, SearchOption.AllDirectories).FirstOrDefault();
            if (file == null)
                // Handle the file not being found
               filepath = "~/Images" + file.Split(seprator, StringSplitOptions.None)[1];
               filepath = filepath.Replace("\\", "/");

            return filepath;
amitesh1989 19-Jun-13 11:56am
My data came from database and here you are creating table with datarow
Bajirao_ 20-Jun-13 1:39am
In page load there is dummy data, write your code for getting data from database. Then bind that datatable to datalist.
amitesh1989 20-Jun-13 3:41am
ok let me try your code
amitesh1989 20-Jun-13 4:15am
i have few question for you prasad
1. File path is the full path of images i store in database?
2.Why we use anchor tag here and why is the image link?
3.What parameter i have to pass in the object of findcontrol?
Bajirao_ 20-Jun-13 4:52am
Ans1 : Let me see some of your full path of files in database. then i will suggest.
Ans2 : Because you provided that code in post.
Ans3 : In findcontrol method you need to pass id of that control which you want to search. make sure it has runat="server".
amitesh1989 20-Jun-13 6:54am
This is one of my image path save in the database IT sub-folder with image as parent folder
amitesh1989 20-Jun-13 7:01am

Go to that link you will see my database
amitesh1989 20-Jun-13 8:33am
hi Prasad i got an error that it GetImagePath function that it can not find the path of the image
Bajirao_ 20-Jun-13 8:41am
In place of below code
string filepath = GetImagePath(Convert.ToString(resultTable.Rows[e.Item.ItemIndex]["ImageName"]));

Try this (if you have path column in table as mentioned in link)

string filepath = "~/" + Convert.ToString(resultTable.Rows[e.Item.ItemIndex]["Path"]);
amitesh1989 20-Jun-13 9:45am
in this if i change my id to ImageName then it will run but didn't show me images
amitesh1989 20-Jun-13 10:06am
hi Prasad the think is that Object objImage = e.Item.FindControl("Image1");
if (objImage != null)
HtmlImage img = objImage as HtmlImage;
img.Src = Imagepath;
img.Width = 100;
img.Height = 100;

when i debbug my code img will show null value that why it wont show images how to change the index of it
Bajirao_ 21-Jun-13 4:17am
if objImage is not null then make sure you have added control "Image1" as <img id="Image1" runat="server" />.
amitesh1989 25-Jun-13 4:38am
i did but its not working Images are not visible in datalist

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