Click here to Skip to main content
15,906,463 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have to fetch single single images on button click but all the images are viewing on image control. I am trying to fetch image one by one on image control after button click

What I have tried:

protected void Page_Load(object sender, EventArgs e)
{
    string[] filesindirectory = Directory.GetFiles(Server.MapPath("~/Images"));
    List<String> images = new List<string>(filesindirectory.Count());

    foreach (string item in filesindirectory)
    {
        images.Add(String.Format("~/Images/{0}", System.IO.Path.GetFileName(item)));
    }

    RepeaterImages.DataSource = images;
    RepeaterImages.DataBind();
}




<asp:Repeater ID="RepeaterImages" runat="server">
    <ItemTemplate>
        <asp:Image ID="Image" runat="server" ImageUrl='<%# Container.DataItem %>' />
    </ItemTemplate>
</asp:Repeater>
Posted
Updated 10-Mar-17 1:28am
Comments
ZurdoDev 10-Mar-17 7:09am    
What?
Rakesh R Surve[RST] 10-Mar-17 8:14am    
I want to fetch single image at a time from perticular folder.
ZurdoDev 10-Mar-17 8:17am    
Repeating yourself is not making this clearer.

1 solution

No need to use Repeater for that, you shall create an image control with two buttons will do the trick.

<asp:Image ID="img"  runat="server" />
       <br />
      <asp:Button Text="Previous" ID="btnPrev" runat="server" OnClick="btnPrev_Click" />
      <asp:Button Text="Next" ID="btnNext" runat="server" OnClick="btnNext_Click" />


protected void Page_Load(object sender, EventArgs e)
       {

           if (!Page.IsPostBack)
           {
               var files = Directory.GetFiles(Server.MapPath("~/Images")).ToList();
               ViewState["Index"] = 0;
               string path = String.Format("~/Images/{0}", System.IO.Path.GetFileName( files[0]));
               img.ImageUrl = path;

           }
       }

       protected void  btnNext_Click(object sender, EventArgs e)
       {
           var files = Directory.GetFiles(Server.MapPath("~/Images")).ToList();
           int index = Convert.ToInt32(ViewState["Index"]);
           int count = files.Count;
           index++;
           if (index < count)
           {
               string path = String.Format("~/Images/{0}", System.IO.Path.GetFileName(files[index]));
               img.ImageUrl = path;
           }
           else
               index = count - 1;

           ViewState["Index"] = index;

       }

       protected void btnPrev_Click(object sender, EventArgs e)
       {
           var files = Directory.GetFiles(Server.MapPath("~/Images")).ToList();
           int index = Convert.ToInt32(ViewState["Index"]);
           int count = files.Count;
           index--;
           if (index > 0)
           {
               string path = String.Format("~/Images/{0}", System.IO.Path.GetFileName(files[index]));
               img.ImageUrl = path;
           }
           else
               index = 0;

           ViewState["Index"] = index;
       }
 
Share this answer
 
Comments
Rakesh R Surve[RST] 10-Mar-17 8:15am    
Sir, Previous Button not working...
ZurdoDev 10-Mar-17 8:17am    
Then debug and fix it. It is very rude to expect other people to do all the work for you.
Karthik_Mahalingam 10-Mar-17 8:24am    
if (index > 0)
if (index > -1)
ZurdoDev 10-Mar-17 8:18am    
+5 for understanding what the OP wanted and providing a good example.
Karthik_Mahalingam 10-Mar-17 8:24am    
Thanks RyanDev

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900