2012-10-06 46 views
1

我使用DataList來顯示數據庫中的一些數據並填充html端的字段。我現在需要根據db字段是否有數據來更改面板的可見性。.net在HTML中返回true如果讀者對象不爲空,否則返回false

我需要能夠顯示面板,如果相關的數據字段有內容,並隱藏它,如果沒有。例如:

<asp:Panel ID="pnlNew" runat="server" Style="margin:0; padding:0; width:42px; height:18px; bottom:5px; right:10px; float:right; position:relative; background:url(../_imgVideoBadge.png) no-repeat;" Visible='<%# Eval("cheese") != null %>' ToolTip="available"></asp:Panel> 

很明顯,這對於可見屬性無效。但希望它給出了我想要實現的想法。任何幫助將不勝感激。

我看到以前的做線沿線的一些例子:

a ?? b:c 

這怎麼可能被應用到上述要求?

在此先感謝。

回答

1

這是我設法制定出解決方案:

(Eval("cheese").ToString().Trim() == String.Empty) ? false : true 

,因爲它似乎結果是一個空字符串,而不是空。

0
protected void Page_Load(object sender, EventArgs e) 
    { 
     if (!Page.IsPostBack)   
     SqlConnection MyConnection; 
     SqlCommand MyCommand; 
     SqlDataReader MyReader; 
     MyConnection = new SqlConnection(); 
     MyConnection.ConnectionString = ConfigurationManager.ConnectionStrings["AppConnectionString1"].ConnectionString; 

     MyCommand = new SqlCommand(); 
     MyCommand.CommandText = "SELECT TOP 10 * From PRODUCTS"; 
     MyCommand.CommandType = CommandType.Text; 
     MyCommand.Connection = MyConnection; 

     MyCommand.Connection.Open(); 
     MyReader = MyCommand.ExecuteReader(CommandBehavior.CloseConnection); 
     if (MyReader != null) 
     { 
      datalist1.DataSource = MyReader; 
      datalist1.DataBind(); 
      pnlNew.Visible = true; 
     } 
     else 
     { 
      pnlNew.Visible = false; 
     } 
     MyCommand.Dispose(); 
     MyConnection.Dispose(); 
    } 
} 
+0

對不起。這真的不能回答這個問題。重點是在html端做到這一點。 –

+0

好吧,也肯定會從HTML端嘗試。 – Chirag

+0

這就是代碼隱藏!除非你很奇怪,否則你不會在html中這樣做。 –

2

Visible='<%# Information.IsDBNull(Eval("cheese")) %>'應返回一個布爾值。

+0

感謝您的回覆,但是「信息」涉及什麼? –

+0

這是一個包含多個靜態幫助器方法的內置類。有關更多詳細信息,請參閱http://msdn.microsoft.com/en-us/library/428tfy8s.aspx。 – pete

相關問題