RockStar
RockStar

Reputation: 87

asp.net image not working

I'm trying to display images through code-behind of asp.net. 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">
    <ItemTemplate>
        <div class="one-third">
            <div>
             <asp:LinkButton ID="linkButton" OnClick="Repeater1_OnClick" runat="server" CommandArgument='<%# Eval("brandId") %>'>
                 <asp:Image ID="brandImage" runat="server" height="250px" width="300px" />
             </asp:LinkButton>
            </div>

        </div>
    </ItemTemplate>

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;
        Repeater1.DataBind();

        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();
        da.Fill(ds);
        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;
        Response.Redirect("~/Pages/GuitarItemsFront.aspx");
    }
}
}

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.

Upvotes: 0

Views: 406

Answers (1)

hardkoded
hardkoded

Reputation: 21695

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/" +
            ((DataRowView)e.Item.DataItem).Row["image"];
    }

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

Upvotes: 1

Related Questions