简体   繁体   中英

Image Control in Datagrid ASP.Net

here is my requirements. my database contain few fields along with image data as bytes. Now i have webpage i can filter the data with few filters and show the images in the grid view along with the data.

I wrote my code as below, all other data is showing fine in the webpage but for Images it is showing blank. in debug mode i verified it showing some bytes of information for image. I am not receiving any error messages as well. I am confused what went wrong with my code.

Any help please? screenshot of my webpage

My class:

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

My Button click event along with method:

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;
    }

My Gridview code in my aspx file:

 <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>

您的模型属性不匹配:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM