Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I am getting the following error

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

but all I am trying to do is inside a ASP.NET REPEATER Control

<% if ( Eval("Message").ToString() == HttpContext.Current.Profile.UserName) %>
<% { %>

           <asp:ImageButton runat="server" etc.... />
<% } %>
like image 496
Dan Avatar asked Apr 03 '10 14:04

Dan


3 Answers

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);
}
like image 170
Steve Avatar answered Oct 23 '22 17:10

Steve


An alternative is this:

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

Then there is no need for code behind.

like image 28
Jen Avatar answered Oct 23 '22 17:10

Jen


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

like image 3
Rohit Arora Avatar answered Oct 23 '22 16:10

Rohit Arora