Binding Eval with an ImageURL in ASP.NET

不问归期 提交于 2019-12-01 22:59:53

simply use

<asp:Image id="abc" ImageUrl =<%# string.Format("~/SiteImages/ram/3/{0}",Eval("imagepath"))%>

imagepath could be from datatable or cs

I personally prefer to do these things in the codebehind directly like

<bri:ThumbViewer ID="thumbViewer" runat="server" ... />

and then in the codebehind you have some initialize or DataBind() method where you write

thumbViewer.ImageUrl= Path.Combine(ImagePath, Name); //or something similar, you have to check

This because especially when you develop in a team it is quite inconvenient and error-prone if people do some bindings in the ASPX code directly using Eval(...) and some in the codebehind. I prefer using the codebehind because then you immediately see what's going on on the page by just looking on your code, while your ASPx code is just for layout, definition of controls (with properties) etc...

string strImagePath = "aPage.aspx";
string pathFormat = "~/SiteImages/ram/3/{0}";
string path = String.Format(path, strImagePath);

Thats a little bit verbose, but you get the idea. What you're looking for is the String.Format method.

You can read more about it over at MSDN -> String.Format

So in your case that would be:

<bri:ThumbViewer Id="Th1"  runat="server" ImageUrl='<%# Eval("Name", String.Format("~/SiteImages/ram/3/{0}", strImagePath)) %>' Height="100px" Width="100px"/>

as long as strImagePath is set to public or protected in your codebehind

Can you just write (and forgive me if this is wrong) if it is constant:

<bri:ThumbViewer ImageUrl='~/SiteImages/ram/3/<%# Eval("Name")%>' Height="100px" Width="100px" Id="Th1"  runat="server"/>

And if it isn't:

<bri:ThumbViewr ImageUrl='<#Eval("ImagePath + Name") %>' ... />

//And in your codebehid:
public property ImagePath { get; set; }
...
ImagePath = "...";
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!