2013-10-22 49 views
0

我有一個問題開始讓我感到沮喪。我使用gridviews創建了一個非常簡單的博客。我被賦予了能夠上傳圖像到帖子的另一個要求。我遇到的問題是如果用戶創建了一個沒有圖片的帖子,圖片控件就會顯示出紅色的x。我嘗試過很多事情都沒有成功。我正在使用自定義處理程序來獲取帖子的圖像。當沒有圖像時,在Gridview中隱藏圖像控件

ImageHandler.ashx

我有一個圖片表,涉及到郵政表。如您所見,它使用QueryString並檢索MessageID或Posts標識,然後顯示圖像。

這是我目前在Posts.aspx圖像控件。

ASP

<asp:Image ID="postImage" runat="server" ImageUrl='<%# "ImageHandler.ashx?mid="+ Eval("MessageID") %>' Width="300px" Height="300px" GenerateEmptyAlternateText="True" /> 

我曾嘗試解決方案here,也是解決here但沒有已成功爲我。我嘗試使用Visible屬性,只顯示不是「null」但結果相同的圖像。如果我需要其他東西,請告訴我!

編輯:這是什麼現在我對ImageHandler.ashx

ImageHandler.ashx

// 1x1 transparent GIF 
     private readonly byte[] GifData = 
     { 
      0x47, 0x49, 0x46, 0x38, 0x39, 0x61, 
      0x01, 0x00, 0x01, 0x00, 0x80, 0xff, 
      0x00, 0xff, 0xff, 0xff, 0x00, 0x00, 
      0x00, 0x2c, 0x00, 0x00, 0x00, 0x00, 
      0x01, 0x00, 0x01, 0x00, 0x00, 0x02, 
      0x02, 0x44, 0x01, 0x00, 0x3b 
     }; 
    SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["dboBlog"].ConnectionString); 
    public void ProcessRequest(HttpContext context) 
    { 
     try 
     { 
      string messageid = context.Request.QueryString["mid"]; 

      conn.Open(); 
      SqlCommand command = new SqlCommand("SELECT Image from BlogImages WHERE Image IS NOT NULL AND MessageID=" + messageid, conn); 
      SqlDataReader dr = command.ExecuteReader(); 

      if (dr.Read()) 
      { 
       context.Response.BinaryWrite((Byte[])dr[0]); 
       conn.Close(); 
       context.Response.End(); 
      } 
      else 
      { 
       context.Response.ContentType = "image/gif"; 
       context.Response.Buffer = false; 
       context.Response.OutputStream.Write(GifData, 0, GifData.Length); 
      } 
     } 
     catch (Exception ex) 
     { 
      return; 
     } 
    } 

我做了更新由運給出的答案這個提供。唯一的問題是,gridview確實有一個樣式,每行是白色和灰色之間的交替顏色。所以有一個白色的盒子,如果沒有圖像,並且是灰色的行,就會顯示出來。

+0

在您提到的以前的解決方案中,您是否嘗試將「已啓用」屬性設置爲false? – IrishChieftain

+0

在圖像控制上?我已經看過它,但我認爲只有EnableTheming和EnableViewState是唯一的啓用選項。 – Humpy

+0

是的,我已經發布它作爲答案。我認爲這可能適合你... – IrishChieftain

回答

0

好了,所以我想出了一個解決方案。我只將一張圖像保存到表格中。在插入時,它會插入圖像,或者如果未插入圖像,則插入「NULL」。然後檢查表是否有空圖像,如果是,則禁用圖像控制。我將下列內容放入Page_Load中。

C#代碼隱藏

if (Page.IsPostBack || !Page.IsPostBack) 
      { 
       foreach (GridViewRow allrows in gvPosts.Rows) 
       { 
        string messageid = ((Label)allrows.FindControl("lblMessageID")).Text; 
        Image image = ((Image)allrows.FindControl("postImage")); 

       conn.Open(); 
       SqlCommand checkNullImage = new SqlCommand("SELECT Image FROM BlogMessages WHERE MessageID=" + messageid, conn); 
       nullImage = Convert.ToString(checkNullImage.ExecuteScalar()); 

       if (nullImage == DBNull.Value.ToString()) 
       { 
        image.Visible = false; 
       } 

       conn.Close(); 
      } 

這是做的最好的方法是什麼?我不確定,但它的工作原理,因爲我一次只顯示10篇文章,它很快就會起作用。

2

如果沒有圖像可用,您可以渲染透明圖像窗體圖像處理程序。

這裏是樣品 -

// 1x1 transparent GIF 
private readonly byte[] GifData = 
{ 
    0x47, 0x49, 0x46, 0x38, 0x39, 0x61, 
    0x01, 0x00, 0x01, 0x00, 0x80, 0xff, 
    0x00, 0xff, 0xff, 0xff, 0x00, 0x00, 
    0x00, 0x2c, 0x00, 0x00, 0x00, 0x00, 
    0x01, 0x00, 0x01, 0x00, 0x00, 0x02, 
    0x02, 0x44, 0x01, 0x00, 0x3b 
}; 

public void ProcessRequest(HttpContext context) 
{ 
    try 
    { 
     ... 
    } 
    catch (Exception ex) 
    { 
     context.Response.ContentType = "image/gif"; 
     context.Response.Buffer = false; 
     context.Response.OutputStream.Write(GifData, 0, GifData.Length); 
    } 
} 
+0

這工作很好。唯一的問題是,在gridview中,行是交替的。白色,灰色,白色,灰色。因此,當添加新帖子時,灰色顯示一個白色正方形。 – Humpy

0

關於你聯繫,請嘗試將圖像控制爲「假」的Enabled屬性以前的解決方案。

Image.Enabled Property

+0

我無法獲得該解決方案的工作。我得到一個'ProjectName.ClassName.Enabled':找不到合適的方法來覆蓋@ [BrowsableAttribute(false)] public override bool Enabled {get;組; } – Humpy

+0

是否在某種容器中進行圖像控制?如果是這樣,並且您正在代碼中設置Enabled屬性,請嘗試使用FindControl()方法。很難說沒有看到你如何設置啓用屬性... – IrishChieftain