C# 在 ASP.NET 中将 Eval 与 ImageURL 绑定
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1135562/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Binding Eval with an ImageURL in ASP.NET
提问by user42348
I'm trying to bind an image using Eval()
with VB.NET and ASP.NET, but am running into issues:
我正在尝试使用Eval()
VB.NET 和 ASP.NET绑定图像,但遇到了问题:
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
在代码隐藏中设置为:
strImagePath ="~/SiteImages/ram/3/"
How can I replace:
我该如何更换:
~/SiteImages/ram/3/{0}
with the variable strImagePath
?
与变量strImagePath
?
回答by Darko Z
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.
这有点冗长,但你明白了。您正在寻找的是 String.Format 方法。
You can read more about it over at MSDN -> String.Format
你可以在 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
只要strImagePath
设置为public
或protected
在您的代码隐藏中
回答by Andrew Backer
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 = "...";
回答by Juri
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
然后在代码隐藏中,您有一些 initialize 或 DataBind() 方法,您可以在其中编写
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...
这是因为,特别是当您在团队中进行开发时,如果人们直接使用 Eval(...) 在 ASPX 代码中进行一些绑定,而在代码隐藏中进行一些绑定,则会非常不方便且容易出错。我更喜欢使用代码隐藏,因为这样您只需查看代码即可立即看到页面上发生的事情,而您的 ASPx 代码仅用于布局、控件定义(带有属性)等...
回答by Juri
simply use
简单地使用
<asp:Image id="abc" ImageUrl =<%# string.Format("~/SiteImages/ram/3/{0}",Eval("imagepath"))%>
imagepath could be from datatable or cs
imagepath 可以来自数据表或 cs