将Eval与ASP.NET中的ImageURL绑定

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

代码段

 <bri:ThumbViewer Id="Th1" runat="server" ImageUrl='' Height="100px" Width="100px" /> 

我在代码隐藏中设置了strImagePath

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

我该如何更换:

 ~/SiteImages/ram/3/{0} 

用变量strImagePath

简单地用

  

imagepath可以来自datatable或cs

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

  

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

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

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

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

这有点冗长,但你明白了。 您正在寻找的是String.Format方法。

您可以在MSDN – > String.Format上阅读更多相关信息

所以在你的情况下,这将是:

  

只要strImagePath在您的代码隐藏中设置为publicprotected

如果它是常数,你可以写(如果这是错的话,请原谅我):

  

如果不是:

  //And in your codebehid: public property ImagePath { get; set; } ... ImagePath = "...";