2

I'm trying to bind an image using Eval() with VB.NET and ASP.NET, but am running into issues:

Code snippet

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

I set strImagePath in the code-behind as:

strImagePath  ="~/SiteImages/ram/3/"

How can I replace:

~/SiteImages/ram/3/{0} 

with the variable strImagePath?

4 Answers 4

6

simply use

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

imagepath could be from datatable or cs

Sign up to request clarification or add additional context in comments.

1 Comment

string.Format(X, Eval(Y)) is the same as Eval(Y, X)
1

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...

Comments

0
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

Comments

0

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 = "...";

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.