Datagrid中的图像控制ASP.Net

时间:2016-02-05 23:06:32

标签: c# asp.net gridview

这是我的要求。我的数据库包含少量字段以及图像数据作为字节。现在我有了网页,我可以使用少量过滤器过滤数据,并在网格视图中显示图像以及数据。

我编写了如下代码,所有其他数据在网页上显示正常,但对于图像,它显示为空白。在调试模式下,我验证它显示了图像的一些字节信息。我也没有收到任何错误消息。我很困惑我的代码出了什么问题。

请帮忙吗?screenshot of my webpage

我的课程:

public class Expense
{
    public string ID { get; set; }
    public DateTime StartTimeStamp { get; set; }
    public double ExpenseAmt { get; set; }
    public byte[] Photo { get; set; }
}

我的按钮点击事件以及方法:

protected void Button1_Click(object sender, EventArgs e)
    {
        List<Expense> exps = getSQLData();
        GridViewImage.DataSource = exps;
        GridViewImage.DataBind();
   }

public List<Expense> getSQLData()
    {
        List<Expense> Expenses = new List<Expense>();
        string cs = ConfigurationManager.ConnectionStrings["test"].ConnectionString;

        using (SqlConnection conn = new SqlConnection(cs))
        {
            SqlCommand cmd = new SqlCommand("Select ID, Start_Timestamp,  Expense_Amt, Receipt_Photo from EXPENSE where id='testing'", conn);
            conn.Open();
            SqlDataReader rdr = cmd.ExecuteReader();
            while (rdr.Read())
            {
                Expense exp = new Expense();

                exp.ID = rdr[0].ToString();
                exp.StartTimeStamp = Convert.ToDateTime(rdr[1].ToString());
                exp.ExpenseAmt = Convert.ToDouble(rdr[2].ToString());
                exp.Photo = (byte[])rdr[3];

                Expenses.Add(exp);

            }
        }

        return Expenses;
    }

我的aspx文件中的Gridview代码:

 <asp:GridView ID="GridViewImage" runat="server" AutoGenerateColumns="false">
<Columns>
    <asp:BoundField DataField="ID" HeaderText="EnterpriseID" />
    <asp:BoundField DataField="StartTimeStamp" HeaderText="StartTimeStamp" />
    <asp:BoundField DataField="ExpenseAmt" HeaderText="ExpenseAmt" />
    <asp:TemplateField HeaderText="Photo">
        <ItemTemplate>
            <asp:Image ID="ReceiptImage" runat="server" Height="500px" Width="500px"
                ImageUrl ='<%# "data:image/jpg;base64," + Convert.ToBase64String((byte[])Eval("Photo")) %>'>

            </asp:Image>
        </ItemTemplate>
    </asp:TemplateField>

</Columns>

1 个答案:

答案 0 :(得分:1)

您的模型属性不匹配:

ImageUrl ='<%# "data:image/jpg;base64," + Convert.ToBase64String((byte[])Eval("Photo")) %>'