title: How to display images using foreach statement in asp.net?
body: <p>To better understand my problem, I will explain first my main goal for the website. My website is a music store which has 6 different brand of guitars(Ibanez, Fender, Gibson, PRS, Musicman and ESP). Now I've created a webpage where it will display the logo for each guitar brands.</p>
  
  <p>Here is the aspx code for displaying the guitar logos:</p>
  
  <pre><code>&lt;%@ Page Title='' Language='C#' MasterPageFile='~/MasterPage.master' 
   AutoEventWireup='true' CodeFile='GuitarBrands.aspx.cs' 
   Inherits='Pages_GuitarBrands' %&gt;
  
   &lt;asp:Content ID='Content1' ContentPlaceHolderID='ContentPlaceHolder1' 
   Runat='Server'&gt;
  
   &lt;% foreach (guitarBrand guitar in brandList) { %&gt;
    &lt;table class="one-third"&gt;
        &lt;tr&gt;
           &lt;th rowspan="3" class="guitarLogoHover"&gt;
          &lt;a href="&lt;%= guitar.page%&gt;"&gt;&lt;img src="&lt;%= guitar.image %&gt;"/&gt;&lt;/a&gt;
       &lt;/th&gt;
        &lt;/tr&gt;
    &lt;/table&gt;
  
    &lt;% } %&gt;
  
   &lt;/asp:Content&gt;
  </code></pre>
  
  <p>Here is the aspx.cs code for displaying the guitar logos:</p>
  
  <pre><code>public partial class Pages_GuitarBrands : System.Web.UI.Page
  {
  public List&lt;guitarBrand&gt; brandList { get; set; }
  private string brandType = "Guitar";
  protected void Page_Load(object sender, EventArgs e)
  {
      brandList = new List&lt;guitarBrand&gt;();
      brandList = ConnectionClassBrands.GetBrandsByType(brandType);
  }
  </code></pre>
  
  <p>The code above successfully displays all 6 guitar logos in the aspx webpage. Now the next thing to do is, when i click a guitar logo, lets say i click the Ibanez logo. it should open a new webpage and display guitars that are available within that brand. The tricky part is, I'm trying to create a single webpage for all guitar brands, which will serve like a template so that i don't need to have many aspx pages per guitar brand. Here is my attempt to this.</p>
  
  <p>Firstly, in my connectionclass code where all the retrieving of data happens. I added a private string field named guitar that will retrieve the guitar brand everytime that it is iterating in the foreach loop.</p>
  
  <p>Code 1 - Here is the new aspx code for displaying the guitar logos. As you can see below, i've created a string field named guitar in ConnectionClassGuitarItems to get the guitar brand.</p>
  
  <pre><code>&lt;%@ Page Title='' Language='C#' MasterPageFile='~/MasterPage.master' 
  AutoEventWireup='true' CodeFile='GuitarBrands.aspx.cs' 
  Inherits='Pages_GuitarBrands' %&gt;
  
  &lt;asp:Content ID='Content1' ContentPlaceHolderID='ContentPlaceHolder1' 
  Runat='Server'&gt;
  
  
   &lt;% foreach (guitarBrand guitar in brandList) { %&gt;
    &lt;table class="one-third"&gt;
        &lt;tr&gt;
           &lt;th rowspan="3" class="guitarLogoHover"&gt;
          &lt;a href="&lt;%= guitar.page%&gt;"&gt;&lt;img src="&lt;%= guitar.image %&gt;"/&gt;&lt;/a&gt;
           &lt;/th&gt;
        &lt;/tr&gt;
    &lt;/table&gt;
   &lt;% ConnectionClassGuitarItems.guitar = guitar.name;%&gt;//newly added code
   &lt;% } %&gt;
  
  &lt;/asp:Content&gt;
  </code></pre>
  
  <p>Code 2 - Here is also the code for the ConnectionClassGuitarItems. The value that the field guitar get from the above. It will supplement the List GetGuitarItems() method so that it can identify what guitar products to retrieve.</p>
  
  <pre><code>public static string guitar;
  public static List&lt;guitarItem&gt; GetGuitarItems()
  {
  
      List&lt;guitarItem&gt; list2 = new List&lt;guitarItem&gt;();
      BrandsDBEntities obj2 = new BrandsDBEntities();
      list2 = (from g in obj2.guitarItems where g.brand == guitar select g).ToList();
  
      return list2;
  }
  </code></pre>
  
  <p>Code 3 - Now here is the aspx.cs code for displaying the guitar products. This is where i would store the retrieved products in another field called List guitarItemList so that i can supplement it in its aspx page.</p>
  
  <pre><code>public partial class Pages_GuitarItems1 : System.Web.UI.Page
  {
  public List&lt;guitarItem&gt; guitarItemList { get; set; }
  protected void Page_Load(object sender, EventArgs e)
  {
      guitarItemList = new List&lt;guitarItem&gt;();
      guitarItemList = ConnectionClassGuitarItems.GetGuitarItems();
  }
  }
  </code></pre>
  
  <p>Code 4 - And finally here is the aspx code for displaying the guitar products.</p>
  
  <pre><code>&lt;%@ Page Title="" Language="C#" MasterPageFile="~/MasterPage.master" 
  AutoEventWireup="true" CodeFile="GuitarItems1.aspx.cs" 
  Inherits="Pages_GuitarItems1" %&gt;
  
  &lt;asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" 
  Runat="Server"&gt;
  
   &lt;% foreach (guitarItem gItem in guitarItemList) { %&gt;
      &lt;div class="one-two"&gt;
          &lt;img src="&lt;%= gItem.itemimage1 %&gt;"/&gt;
          &lt;div class="content"&gt;
              &lt;div id="label"&gt;&lt;%= gItem.brand %&gt; &lt;%= gItem.model %&gt;&lt;/div&gt;
          &lt;/div&gt;
      &lt;/div&gt;
   &lt;% } %&gt;
  
  &lt;/asp:Content&gt;
  </code></pre>
  
  <p><strong>Now here is the problem</strong>
  It is not displaying the right guitar products according to its brand. Since i've only made a single webpage for each brand, it is only displaying ESP guitar products. For example if i click the Ibanez logo, it should display Ibanez products. But in this case, when i click the Ibanez logo, it will show ESP guitar products. Same as the other brand logos, when i click it, it will only display ESP guitar products. What is wrong with the new code that i have implemented?</p>
  
  <p>From my understanding, the foreach loop will iterate the guitar brands in this order(Ibanez,Fender,Gibson,PRS,Musicman and ESP). Please help me guys on solving this one. I've already tried several variations but its still having the same results.   </p>