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.... />
<% } %>

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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);
}

Method 2

An alternative is this:

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

Then there is no need for code behind.

Method 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

Method 4

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(); %>


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x