38

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.... />
<% } %>
mattbasta
  • 13,492
  • 9
  • 47
  • 68
Dan
  • 383
  • 1
  • 3
  • 4

4 Answers4

51

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);
}
Steve
  • 5,802
  • 12
  • 54
  • 76
  • nice, I like that solution but what is Container.DataItem. While searching for an answer online I came across it also but cant seem to reference it in my project thanks for the response – Dan Apr 03 '10 at 15:21
  • Cool that worked... i used the following – Dan Apr 03 '10 at 15:50
  • Yep, you can leave out the Container.DataItem, see http://msdn.microsoft.com/en-us/library/system.web.ui.databinder.eval.aspx – Steve Apr 03 '10 at 16:27
  • 5
    For a similar situation I had to fully specify DataBinder like: `DataBinder.Eval(Container.DataItem,"Message")`. – John K Jan 09 '13 at 19:19
  • I want to use IF ELSE in repeater! how am I supposed to do that? – Shahriar Rahman Zahin Mar 12 '20 at 11:26
28

An alternative is this:

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

Then there is no need for code behind.

Jen
  • 319
  • 2
  • 2
  • Works Great. You can even use this in stead of an If statement. Just put two of those controls in with the Visible parameters for one as == and the other as != (or whatever you logic is) – Ruan Apr 30 '13 at 12:43
3

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

Rohit Arora
  • 2,246
  • 2
  • 24
  • 40
0

Another way to implement it:

public string nonImage() {
    string imgTag = "", Article_OwnerID = "", Article_ID = "", Article_OwnerType = "", imgSrc = "";
    DataTable DtArticles = SE_Article.GetArticlesList(UserID, UserID, ProfileType, CounterOfPage, CountPerPage, (short) SE_Action.OwnerType.user, SE_Security.CheckInjection(TxtSearch.Text.Trim()), CategoryID, "all_articles", DrpOrderBy.SelectedValue, DrpSort.SelectedValue);
    if (DtArticles != null && DtArticles.Rows.Count > 0) {
        Article_OwnerID = DtArticles.Rows[0]["Article_OwnerID"].ToString();
        Article_ID = DtArticles.Rows[0]["Article_ID"].ToString();
        Article_OwnerType = DtArticles.Rows[0]["Article_OwnerType"].ToString();
    }
    if (SE_Article.GetArticleCover(Convert.ToInt32(Article_OwnerID), Convert.ToInt32(Article_ID), Convert.ToInt16(Article_OwnerType)) != System.Configuration.ConfigurationManager.AppSettings["NoPhotoArticleThumb"]) {
        imgSrc = SE_Article.GetArticleCover(Convert.ToInt32(Article_OwnerID), Convert.ToInt32(Article_ID), Convert.ToInt16(Article_OwnerType));
        imgTag = "<img class='img_article_cover' src='" + imgSrc + "' alt='مقاله" + Article_ID + "' />";
    }
    return imgTag;
 }


 <% nonImage(); %>
Rohit Arora
  • 2,246
  • 2
  • 24
  • 40
Danial
  • 9
  • 1