2010-04-03 52 views

回答

42

語法

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

你可以做類似

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

並在你的代碼隱藏中:

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

不錯,我喜歡這樣的解決方案,但什麼是的Container.DataItem。雖然在網上搜索一個答案我碰到它,但似乎沒有參考它在我的項目 感謝您的迴應 – Dan 2010-04-03 15:21:39

+0

涼爽的工作...我使用以下 Dan 2010-04-03 15:50:57

+0

是的,你可以省略Container.DataItem,參見http://msdn.microsoft.com/en-us/library/system.web.ui。 databinder.eval.aspx – Steve 2010-04-03 16:27:59

28

另一種方法是這樣的:

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

那麼就沒有必要對後面的代碼。

+0

工程很棒。你甚至可以用它來代替If語句。只需將其中兩個控件與Visible參數中的一個設爲==,另一個設爲!=(或任何您的邏輯) – Ruan 2013-04-30 12:43:19

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

爲時已晚,但我想回答這個問題在我的方式,我用什麼來實現它:

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

現在這僅會圖像按鈕,如果消息等於用戶名。

這可能會幫助其他人在相同的情況。

在我的情況,我需要檢查空和空字符串...所以我實現了這樣如下:

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

感謝