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.
Best Answer
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)
.