尝试在 asp:Image 控件的 ImageUrl 属性中嵌入 DataItem

发布于 2024-08-06 07:43:52 字数 540 浏览 5 评论 0原文

我在中继器中有以下图像控件。我试图让用户名在 ~/profilepics/ 和 .jpg 之间渲染,但我得到以下渲染输出

/profilepics/%3C%25#DataBinder.Eval(Container.DataItem,%20%22usernameFrom%22)% 20%25%3E.jpg

这是标记,

<asp:Image ID="Image1" runat="server" ImageUrl='~/profilepics/<%#DataBinder.Eval(Container.DataItem, "username") %>.jpg' />

我也尝试过相同的操作,但使用双引号并得到相同的结果。

<asp:Image ID="Image1" runat="server" ImageUrl="~/profilepics/<%#DataBinder.Eval(Container.DataItem, "username") %>.jpg" />

I have the following Image control within a repeater. I'm trying to get the username to render in between ~/profilepics/ and .jpg but I get the following rendered output

/profilepics/%3C%25#DataBinder.Eval(Container.DataItem,%20%22usernameFrom%22)%20%25%3E.jpg

Here is the markup

<asp:Image ID="Image1" runat="server" ImageUrl='~/profilepics/<%#DataBinder.Eval(Container.DataItem, "username") %>.jpg' />

I have also tried the same but with double quotes and get the same result.

<asp:Image ID="Image1" runat="server" ImageUrl="~/profilepics/<%#DataBinder.Eval(Container.DataItem, "username") %>.jpg" />

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

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

发布评论

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

评论(2

蝶舞 2024-08-13 07:43:52

您不能在服务器控件内使用数据绑定语法。您是否尝试过使用普通的 img HTML 标签?

<img src='/profilepics/<%#Eval("username") %>.jpg' />

You can't use the databinding syntax inside a server control. Have you tried with a plain img HTML tag instead?

<img src='/profilepics/<%#Eval("username") %>.jpg' />
恬淡成诗 2024-08-13 07:43:52

这样是行不通的。 Image WebControl 将不会采用此类标记。
常见的方法是使用 ItemDataBound 事件将图像源分配给转发器项中的每个图像控件。

如果您想使用这些标记,请不要使用 WebControl,请尝试以下操作:

<img src='<%ResolveClientUrl("~/profilepics")%>/<%#DataBinder.Eval(Container.DataItem, "username") %>.jpg' />

免责声明:我没有测试上面的确切代码,但这个概念应该可行。

It will not work this way. The Image WebControl will not take such markup.
The common method is to use ItemDataBound event to assign the image source to each image control within a repeater item.

If you want to use those markups, do not use WebControl, try this:

<img src='<%ResolveClientUrl("~/profilepics")%>/<%#DataBinder.Eval(Container.DataItem, "username") %>.jpg' />

Disclaimer: I did not test the exact code above but the concept should work.

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