Click here to Skip to main content
15,885,435 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:

C#
<asp:DataList ID="dlImages" runat="server" RepeatColumns="3" CellPadding="3" 
                    style="margin-left: 0px">
<ItemTemplate>
<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" />
</a> 
</ItemTemplate>
<ItemStyle BorderColor="Brown" BorderStyle="dotted" BorderWidth="3px" HorizontalAlign="Center" VerticalAlign="Bottom" />
</asp:DataList>


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

Change
C#
href='<%# Eval("ImageName","~/Images/{0}") %>'
Into
C#
href='<%# Eval("ImageName","~/{0}") %>'
and store the folder name in the variable you insert for {0}.
 
Share this answer
 
Comments
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"
onitemdatabound="dlImages_ItemDataBound">


Object objImageLink = e.Item.FindControl("imageLink");
if (objImageLink != null)
{
HtmlAnchor anchor = objImageLink as HtmlAnchor;
anchor.HRef = filepath;
anchor.DataBind();
}
Quote:
// 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.NET
<asp:datalist id="dlImages" runat="server" repeatcolumns="3" cellpadding="3" xmlns:asp="#unknown">
        Style="margin-left: 0px"  
        onitemdatabound="dlImages_ItemDataBound">
        <itemtemplate>
            <a id="imageLink">
                rel="lightbox[Brussels]" runat="server">
                <img id="Image1"  runat="server" />
            </a>
        </itemtemplate>
        <itemstyle bordercolor="Brown" borderstyle="dotted" borderwidth="3px" horizontalalign="Center">
            VerticalAlign="Bottom" />
    </itemstyle></asp:datalist>

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

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

            resultTable.Columns.Add("ImageName");
            resultTable.Columns.Add("Description");
            resultTable.Columns.Add("ID");

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

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

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

            dlImages.DataSource = resultTable;
            dlImages.DataBind();

        }

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

                // 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;
                    img.DataBind();
                }
            
        }
       // 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
            }
            else
            {
               filepath = "~/Images" + file.Split(seprator, StringSplitOptions.None)[1];
               filepath = filepath.Replace("\\", "/");
            }

            return filepath;
        }
    }
 
Share this answer
 
v2
Comments
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".

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