数据绑定方法(如Eval(),XPath()和Bind())只能在数据绑定控件的上下文中使用

时间:2010-04-03 14:44:53

标签: asp.net repeater eval

我收到以下错误

  

数据绑定方法(如Eval(),XPath()和Bind())只能在数据绑定控件的上下文中使用。

但我想要做的就是在ASP.NET REPEATER Control

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

           <asp:ImageButton runat="server" etc.... />
<% } %>

4 个答案:

答案 0 :(得分:45)

语法是

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

您可以执行类似

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

并在你的代码隐藏中:

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

答案 1 :(得分:27)

另一种选择是:

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

然后不需要代码。

答案 2 :(得分:2)

为时已晚,但我想以我的方式回答它,我过去常常实现它:

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

现在,如果消息等于用户名,则仅显示图像按钮。

这可能会帮助处于相同情况的其他任何人。

在我的情况下,我需要检查null和空字符串...所以我实现如下:

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

由于

答案 3 :(得分:1)

实现它的另一种方法:

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