asp.net - How to get relative path of image for src attribute -


i want display list of images on asp.net web page. pictures located in folder. aspx code looks this

   <asp:listview runat="server" id="lvpicturepaths">    <itemtemplate>      <img src="<%# container.dataitem %>" />    </itemtemplate>     </listview> 

in code behind have:

  private void getimagepaths()    {       list<string> pathforpictures=new list<string>();     var path=server.mappath("~/images/");   foreach(var pp in directory.getfiles(path))     {     pathforpictures.add(pp);     }     lvpicturepaths.datasource=pathforpictures;     lvpicturepath.databind();  } 

the problem src attribute of img tag need relative path, localhost/images... like: c:\inetpub\wwwroot\images\image1.jpg

you can use:

pathforpictures.add(     page.resolveclienturl(         system.io.path.combine(             "~/images/",             system.io.path.getfilename(pp)         )     ) ); 

or instead of doing loop:

private void getimagepaths() {     const string path = "~/images/";     var pictures =         directory.getfiles(server.mappath(path))             .select(p =>                  page.resolveclienturl(path.combine(path, path.getfilename(p))));     lvpicturepaths.datasource = pictures;     lvpicturepath.databind(); } 

Comments

Popular posts from this blog

Detect support for Shoutcast ICY MP3 without navigator.userAgent in Firefox? -

web - SVG not rendering properly in Firefox -

java - JavaFX 2 slider labelFormatter not being used -