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
Post a Comment