在 ASP.NET 中将 Eval 与 ImageURL 绑定

发布于 2024-07-27 13:59:33 字数 476 浏览 0 评论 0原文

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

代码片段

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

我在代码中设置了 strImagePath -后面的 as:

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

如何

~/SiteImages/ram/3/{0} 

用变量 strImagePath 替换:?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

兔小萌 2024-08-03 13:59:34
string strImagePath = "aPage.aspx";
string pathFormat = "~/SiteImages/ram/3/{0}";
string path = String.Format(path, strImagePath);

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

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

所以在你的情况下是:

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

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

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

高冷爸爸 2024-08-03 13:59:34

如果它是恒定的,你可以写(如果这是错误的,请原谅我):

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

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 = "...";
月朦胧 2024-08-03 13:59:33

只需使用

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

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

simply use

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

imagepath could be from datatable or cs

千と千尋 2024-08-03 13:59:33

我个人更喜欢直接在代码隐藏中执行这些操作

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

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

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

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文