Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a databound control Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a databound control asp.net asp.net

Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a databound control


The syntax is

<%# Eval("...") %>

You could do something like

<asp:ImageButton Visible='<%# ShowImg(Eval(Container.DataItem,"Message")) %>' />

and in your codebehind:

boolean ShowImg(string msg){     return (msg == HttpContext.Current.Profile.UserName);}


An alternative is this:

<asp:ImageButton runat="server" Visible='<%# Eval("Message").ToString() == HttpContext.Current.Profile.UserName %>' />

Then there is no need for code behind.


Its too late but i would like to answer it in my way, what i used to achieve it:

<%# Eval("Message").toString()== HttpContext.Current.Profile.UserName)?"<asp:ImageButton runat="server" etc.... />" :""%>

Now this will only show image button if Message is equal to username.

This might help any one else in same situation.

In my situation i needed to check null and empty string...so i implemented like this below:

<%# Eval("DateString")!= null && Eval("DateString")!= ""? "<span class='date'>"+Eval("DateString") + "</span>":"" %>

Thanks