2

我正在尝试使用Eval()VB.NET 和 ASP.NET 绑定图像,但遇到了问题:

代码片段

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

strImagePath在代码隐藏中设置为:

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

我该如何更换:

~/SiteImages/ram/3/{0} 

与变量strImagePath

4

4 回答 4

5

只需使用

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

图像路径可能来自数据表或 cs

于 2010-05-08T21:13:22.687 回答
1

我个人更喜欢直接在代码隐藏中做这些事情

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

然后在代码隐藏中你有一些你写的初始化或 DataBind() 方法

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

这是因为特别是当您在团队中开发时,如果人们直接使用 Eval(...) 在 ASPX 代码中进行一些绑定,而在代码隐藏中进行一些绑定,则非常不方便且容易出错。我更喜欢使用代码隐藏,因为这样您只需查看代码即可立即看到页面上发生了什么,而您的 ASPx 代码仅用于布局、控件定义(带有属性)等......

于 2009-07-16T06:09:42.913 回答
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

于 2009-07-16T05:41:31.800 回答
0

如果它是恒定的,你能不能写(如果这是错的,请原谅我):

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

如果不是:

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

//And in your codebehid:
public property ImagePath { get; set; }
...
ImagePath = "...";
于 2009-07-16T06:04:25.187 回答