开发者

Eval function is C#

开发者 https://www.devze.com 2023-01-05 19:19 出处:网络
I need some help in understanding what the Eval bit does (Just started learning C#.net): <asp:Image

I need some help in understanding what the Eval bit does (Just started learning C#.net):

<asp:Image 
 ID="Image1" 
 ImageUrl='<%# Eval("Name", "~/U开发者_JS百科ploadImages/{0} %>'
 ...

The image is in a datalist repeater which has been binded to a folder containing images files.

I'm confused with the "Name" and {0}.. what is the significance of these and in what situation can I change them.


The Eval statement in an aspx or ascx file is usually used to dynamically evaluate a binding statement within the context of the item bound to the current row in a databound control.

The first parameter is the property/field to bind to on the row. The second parameter is an optional format string. {0} will be replace with the value of the Name property in rendering the output text.

Here's the relevant doc. Enjoy!


That is a format string. Whatever is evaluated by the property, call it evalResult, of the first parameter to eval is passed through a String.Format("~/UploadImages/{0}", evalResult)

So, if the value in your dataset field for name is "Steve.jpg", your grid will show:
<img src="/UploadImages/Steve.jpg" ... />

For more information on Eval, check out msdn: http://msdn.microsoft.com/en-us/library/2d76z3ck.aspx


In this case, the content of "Name" will be inserted in the placeholder {0}.

This way if you have

Name = "ImageName"

Your ImageUrl will be

ImageUrl="~/UploadImages/ImageName"

This is useful in dynamic cenarios because for each object the repeater will change the Name property accordingly to form the image URL.

0

精彩评论

暂无评论...
验证码 换一张
取 消