如何在代码隐藏中调用Eval?
我到底如何从代码隐藏中调用 Eval() ?我知道这是 DataBinder.Eval(Container.DataItem, "name") 之类的前端快捷方式,但我无法编译这个该死的东西。我认为我缺少正确的方法参数。我想把这样的东西:
<asp:Image ID="imgLogo" runat="server" ImageUrl='<%# Eval("name") %>'/>
变成:
<asp:Image ID="imgLogo" runat="server" ImageUrl='<%# GetImagePath(???) %>'/>
w/代码隐藏:
protected string GetImagePath(????)
{
//some code
return "some/logical/path" + Eval("name");
}
asp:Image 控件位于绑定到 SqlDataReader 的中继器中。
How the heck do I call Eval() from codebehind? I know this is a front-end shortcut for something like DataBinder.Eval(Container.DataItem, "name"), but I cannot get this damn thing to compile. I am missing the proper method parameters, I think. I want to turn something like this:
<asp:Image ID="imgLogo" runat="server" ImageUrl='<%# Eval("name") %>'/>
into:
<asp:Image ID="imgLogo" runat="server" ImageUrl='<%# GetImagePath(???) %>'/>
w/ code-behind:
protected string GetImagePath(????)
{
//some code
return "some/logical/path" + Eval("name");
}
The asp:Image control is in a repeater that is bound to a SqlDataReader.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
Eval
是Page
上的一个方法(实际上是在TemplateControl
上,这意味着它也存在于UserControl
上)。因此,您不需要传递任何特殊参数:注意:在数据绑定时只能调用
Eval
,但<%# Eval(...) %> 的情况隐式如此;
也是如此,所以这不会造成问题。如果您想要绑定到的实际对象,请使用
GetDataItem()
,因为Eval(x)
基本上意味着DataBinder.Eval(GetDataItem(), x )
。Eval
is a method onPage
(onTemplateControl
actually, which means that it's also present onUserControl
). So you don't need to pass any special parameters:Caution: you can only call
Eval
while databinding, but that's implicitly the case for<%# Eval(...) %>
too, so that shouldn't pose a problem.If you want the actual object you're binding to, use
GetDataItem()
, asEval(x)
basically meansDataBinder.Eval(GetDataItem(), x)
.你有没有尝试过类似的事情:
和
Have you tried something like:
and
另一种简单的方法是使用采用格式的 Eval 重载,aspx 变为:
Another simple way to do this is to use the overload of Eval that takes a format, the aspx becomes: