title: How to only display certain images in a folder into a Repeater in ASP.NET
body: <p>I have a Repeater that takes all my images in a folder and display it. But what code changes must I make to only allow lets say Image1.jpg and Image2.jpg to be displayed in my repeater. I don"t want the repeater to display ALL the images in my folder.</p>
  
  <p>My Repeater</p>
  
  <pre><code>&lt;asp:Repeater ID="repImages" runat="server" OnItemDataBound="repImages_ItemDataBound"&gt;
  &lt;HeaderTemplate&gt;&lt;p&gt;&lt;/HeaderTemplate&gt;
  &lt;ItemTemplate&gt;
      &lt;asp:HyperLink ID="hlWhat" runat="server" rel="imagebox-bw"&gt;
      &lt;asp:Image ID="imgTheImage" runat="server" /&gt;
      &lt;/asp:HyperLink&gt;
  &lt;/ItemTemplate&gt;
  &lt;FooterTemplate&gt;&lt;/p&gt;&lt;/FooterTemplate&gt;
  &lt;/asp:Repeater&gt;
  </code></pre>
  
  <p>My Code behind - PAGE LOAD</p>
  
  <pre><code>protected void Page_Load(object sender, EventArgs e)
      {
          if (!IsPostBack)
          {
              string sBasePath = System.Web.HttpContext.Current.Request.ServerVariables["APPL_PHYSICAL_PATH"];
              if ( sBasePath.EndsWith("\\"))
                  sBasePath = sBasePath.Substring(0,sBasePath.Length-1);
  
              sBasePath = sBasePath + "\\" + "pics";
  
              System.Collections.Generic.List&lt;string&gt; oList = new System.Collections.Generic.List&lt;string&gt;();
              foreach (string s in System.IO.Directory.GetFiles(sBasePath, "*.*"))
              {
                  //We could do some filtering for example only adding .jpg or something
                  oList.Add( System.IO.Path.GetFileName( s ));
  
              }
              repImages.DataSource = oList;
              repImages.DataBind();
          }
  
      }
  </code></pre>
  
  <p>My Code behind - Repeater's ItemDataBound event code</p>
  
  <pre><code>protected void repImages_ItemDataBound(object sender, RepeaterItemEventArgs e)
      {
          if (e.Item.ItemType == ListItemType.AlternatingItem ||
              e.Item.ItemType == ListItemType.Item)
          {
              string sFile = e.Item.DataItem as string;
  
              //Create the thumblink
              HyperLink hlWhat = e.Item.FindControl("hlWhat") as HyperLink;
              hlWhat.NavigateUrl = ResolveUrl("~/pics/" + sFile  );
              hlWhat.ToolTip = System.IO.Path.GetFileNameWithoutExtension(sFile);
              hlWhat.Attributes["rel"] = "imagebox-bw";
  
              Image oImg = e.Item.FindControl("imgTheImage") as Image;
              oImg.ImageUrl = ResolveUrl("~/createthumb.ashx?gu=/pics/" + sFile + "&amp;xmax=100&amp;ymax=100" );
  
  
          }
  
      }
  </code></pre>
  
  <p><strong>ANSWER:</strong></p>
  
  <p>My updated Page Load</p>
  
  <pre><code>protected void Page_Load(object sender, EventArgs e)
      {
          if (!IsPostBack)
          {
              string sBasePath = System.Web.HttpContext.Current.Request.ServerVariables["APPL_PHYSICAL_PATH"];
              if ( sBasePath.EndsWith("\\"))
                  sBasePath = sBasePath.Substring(0,sBasePath.Length-1);
  
              sBasePath = sBasePath + "\\" + "pics";
  
              System.Collections.Generic.List&lt;string&gt; oList = new System.Collections.Generic.List&lt;string&gt;();
  
              string[] extensions = { "*.jpg", "*.png" };
  
              List&lt;string&gt; files = new List&lt;string&gt;(); 
  
              foreach (string filter in extensions) 
              {
                  files.AddRange(System.IO.Directory.GetFiles(sBasePath, filter)); 
                  oList.Add(System.IO.Path.GetFileName(filter));
              }
  
  
              repImages.DataSource = oList;
              repImages.DataBind();
          }
  </code></pre>