我有一個問題開始讓我感到沮喪。我使用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確實有一個樣式,每行是白色和灰色之間的交替顏色。所以有一個白色的盒子,如果沒有圖像,並且是灰色的行,就會顯示出來。
在您提到的以前的解決方案中,您是否嘗試將「已啓用」屬性設置爲false? – IrishChieftain
在圖像控制上?我已經看過它,但我認爲只有EnableTheming和EnableViewState是唯一的啓用選項。 – Humpy
是的,我已經發布它作爲答案。我認爲這可能適合你... – IrishChieftain