2010-08-24 78 views
2

我有有图像控制有时这个图像控制hadnot数据,我想隐藏它,当它没有数据时,我做了代码,但它显示为图像没有数据(不可用),请任何人帮我的Repeater控制。 注:数据来自数据的基础上使图只好标识从数据库中,但没有值(NULL)如何在没有URL的情况下隐藏图像控件?

protected void DLHome_ItemDataBound(object sender, RepeaterItemEventArgs e) 
{ 

    Image Img = e.Item.FindControl("ModelLogo") as Image; 
    using (SqlConnection con = Connection.GetConnection()) 
    { 
     string Sql = "Select Logo From Model"; 
     SqlCommand com = new SqlCommand(Sql, con); 
     com.CommandType = CommandType.Text; 
     SqlDataReader dr = com.ExecuteReader(); 
     if (dr.Read()) 
     { 
      string Img2 = dr["Logo"].ToString(); 
      if (Img2 == System.DBNull.Value.ToString()) 
      { 
       Img.Visible = false; 
      } 
     } 


    } 



} 

回答

1

一个更好的方式来做到这将是检索原始查询中的徽标数据用于填充您的中继器。然后,您将能够基于该数据在图像上设置Visible属性,而不必在每行绑定时执行新的数据库查询。这会提高你的页面的性能。

关于你已经发布的代码,你不及格的图片ID给SqlCommand

string Sql = "Select Logo From Model where ImageId = @ImageId"; 

    //Assuming List<ImageModel> is what you are binding to the repeater 
    ImageModel model = e.Item.DataItem as ImageModel; 
    com.Parameters.Add("@ImageId", SqlDbType.Int32, model.ImageId); 
1

难道是财产已经分配了某处一个空字符串?尝试使用string.IsNullOrEmpty代替:

if (string.IsNullOrEmpty(Img.ImageUrl)) 
{ 
    Img.Visible = false; 
} 

作为一个侧面说明,这可以缩短(选择何种风格最适合你):

Img.Visible = !string.IsNullOrEmpty(Img.ImageUrl); 
+0

注:数据来自数据的基础上使图只好标识从数据库中,但没有值(NULL) – Myworld 2010-08-24 07:03:16

相关问题