RockStar RockStar - 1 year ago 50
ASP.NET (C#) Question image not working

I'm trying to display images through code-behind of Unfortunately it doesn't show anything and i tried looking for similar problem as mine, but i really can't find an answer. Take note, I'm still a beginner in this.

Here is the code for my aspx file:

<asp:Content ID='Content1' ContentPlaceHolderID='ContentPlaceHolder1' Runat='Server'>

<asp:Repeater ID="Repeater1" runat="server">
<div class="one-third">
<asp:LinkButton ID="linkButton" OnClick="Repeater1_OnClick" runat="server" CommandArgument='<%# Eval("brandId") %>'>
<asp:Image ID="brandImage" runat="server" height="250px" width="300px" />


And here is the code for the aspx.cs file:

using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Pages_GuitarBrands : System.Web.UI.Page

protected void Page_Load(object sender, RepeaterItemEventArgs e)
if (!IsPostBack) {
DataSet ds = GetData();
Repeater1.DataSource = ds;

Image image = e.Item.FindControl("image") as Image;
image.ImageUrl = "../Images/Guitar Brands/"+image;


private DataSet GetData()
string CS = ConfigurationManager.ConnectionStrings["musicStoreConnection"].ConnectionString;
using (SqlConnection con = new SqlConnection(CS))
SqlDataAdapter da = new SqlDataAdapter("SELECT * FROM brand", con);
DataSet ds = new DataSet();
return ds;


protected void Repeater1_OnClick(object sender, EventArgs e)
LinkButton link = (LinkButton)sender;
if (link != null) {
int id = int.Parse(link.CommandArgument);
string brandName = ConnectionClassBrands.GetBrandById(id);
ConnectionClassGuitarItems.guitarName = brandName;

In the aspx.cs file, I tried to access the ID of asp:Image which is brandImage, but when i type it in the page_load method, it does not recognize it. So i tried doing something and setting up the imageurl, which you can see the code that i have made in the page_load method. It ended up not showing any images.

Also just for reference, the reason why I try to concatenate image to the url like this ->| image.ImageUrl = "../Images/Guitar Brands/"+image; | is because in my database, i have an image column where it contains the jpg file.

Please give advice in this matter. Feel free to suggest solutions.

Answer Source

The proper way to implement that is using the ItemDataBound event:

First, attach to the event:

<asp:Repeater ID="Repeater1" runat="server" OnItemDataBound="repeater_ItemDataBound"> 

And then in the event you just find the control as you are trying to do in the Page_Load:

    protected void repeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
        Image img = e.Item.FindControl("brandImage") as Image;
        img.ImageUrl = "../Images/Guitar Brands/" +

I think that DataItem is a DataRowView in your scenario, if it's different let me know and I'll change the example.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download