没有图像时隐藏Gridview中的图像控件

时间:2013-10-22 14:15:53

标签: c# asp.net image gridview

我有一个问题开始让我感到沮丧。我使用gridviews创建了一个非常简单的博客。我被赋予了另一个能够将图像上传到帖子的要求。我遇到的问题是,如果用户创建的帖子没有图片,则图像控件无论如何都会显示红色的x。我尝试过多次没有成功的事情。我正在使用自定义处理程序来获取帖子的图像。

ImageHandler.ashx

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

            dr.Read()
            context.Response.BinaryWrite((Byte[])dr[0]);
            conn.Close();
            context.Response.End();
        }
        catch (Exception ex)
        {
            return;
        }
    }

我有一个与Post表相关的图像表。如您所见,它使用QueryString并检索MessageID或Posts id,然后显示图像。

这是我目前在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;
        }
    }

我确实更新了Win提供的答案。唯一的问题是gridview确实有一个样式,每行在白色和灰色之间交替颜色。因此,如果没有图像,则会出现一个白色框,并且是灰色的行。

3 个答案:

答案 0 :(得分: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);
    }
}

答案 1 :(得分:0)

关于您链接的先前解决方案,请尝试将图像控件的Enabled属性设置为“false”。

Image.Enabled Property

答案 2 :(得分: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个帖子,所以它很快就能运行。