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

前端 未结 4 1538
借酒劲吻你
借酒劲吻你 2020-11-30 07:33

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.

<
相关标签:
4条回答
  • 2020-11-30 07:46

    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

    0 讨论(0)
  • 2020-11-30 07:51

    An alternative is this:

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

    Then there is no need for code behind.

    0 讨论(0)
  • 2020-11-30 07:55

    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);
    }
    
    0 讨论(0)
  • 2020-11-30 07:58

    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(); %>
    
    0 讨论(0)
提交回复
热议问题