Click here to Skip to main content
15,904,288 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi how to display images in folder dynamically
the folder contains 100 images all are *.jpg

I am used this

C#
ArrayList pics = new ArrayList();
        string html;
     
           DirectoryInfo dirs = new DirectoryInfo(Server.MapPath("~/images"));
         html=dirs.GetFiles("*.jpg").Count().ToString ();
         foreach (FileInfo s in dirs.GetFiles("*.jpg"))
         {

             string srt = s.Name;
             string  re="rel=";
             string wt="width=";
             string ht = "height=";
            html= ""+"<img src="+"/images/"+srt+" "+wt+"50"+" "+ ht+"50" + "/>"+"";
              pics.Add(html);
           
   }datalist.DataSource =pics;
        datalist .DataBind ();


my aspx page code is
XML
html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>


</head>
<body>
    <form id="form1" runat="server">
    <div>
    <div >
        <asp:DataList ID="dlpictures" runat="server">
        </asp:DataList>
        </div>
    </div>
    </form>
</body>
</html>



but it not display images
Posted
Updated 3-Jan-12 23:39pm
v3
Comments
Nigam Patel 4-Jan-12 5:30am    
can you please post your aspx page code?
m@dhu 4-Jan-12 5:35am    
You have wt="width=" and you gave wt+"=50" which implies width=="50". I think something is wrong here ;)

1 solution

Try below solution

in aspx code
<asp:DataList ID="dtlist" runat="server">
        <ItemTemplate>
            <asp:Image ID="imgsource" ImageUrl='<%# Eval("ImageUrl") %>' runat="server" />
        </ItemTemplate>
    </asp:DataList>


in code behind page

DataTable dt = new DataTable()
           DataColumn dc = new DataColumn("ImageUrl");
           dt.Columns.Add(dc);

            ArrayList pics = new ArrayList();
       string html;
       DirectoryInfo dirs = new DirectoryInfo(Server.MapPath("~/images"));
       html=dirs.GetFiles("*.jpg").Count().ToString ();
       foreach (FileInfo s in dirs.GetFiles("*.jpg"))
       {

           DataRow dr = dt.NewRow();
           dr[0] = s.Name;
           // you change your code as per need .
           dt.Rows.Add(dr);
       }
                   dtlist.DataSource =dt;
                  dtlist .DataBind ();

               }


hope this will help:)
 
Share this answer
 
v2
Comments
visnumca123 4-Jan-12 5:49am    
this is right!
kvns2608 4-Jan-12 5:50am    
it shows an error

server tag is not formed
Nigam Patel 4-Jan-12 5:51am    
i update the solution please take it

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