I am using an image slider with 7 images. I have a folder in my ASP.NET Web Forms application called Slider with 50 images in it. Every time I refresh or visit the page I want the slider to be populated with 7 different images every time from the 50 images in the folder.
ASPX
<div class="slider">
<div id="slider1">
<% Response.Write("<img src='" + chooseImage() + "' />");%>
<%-- <img border="0" src="Slider/B50.JPG" width="850" height="637" />
<img border="0" src="Slider/B51.JPG" width="850" height="637" />
<img border="0" src="Slider/C47.JPG" width="850" height="637" />
<img border="0" src="Slider/C43.JPG" width="850" height="637" />
<img border="0" src="Slider/E2.JPG" width="850" height="637" />
<img border="0" src="Slider/W5.JPG" width="850" height="637" />
<img border="0" src="Slider/M21.JPG" width="850" height="637" />--%>
</div>
<ul id="thumb"></ul>
<div id='next' class="slider_next">
<img border="0" src="Images/next.png" width="57" height="57" alt="next image" /></div>
<div id='prev' class="slider_prev">
<img border="0" src="Images/prev.png" width="57" height="57" alt="previous image" /></div>
</div>
</div>
Before I just had a direct path to 7 images in the folder but I want it to be randomized.
<% Response.Write("<img src='" + chooseImage() + "' />");%>
ASPX.CS
protected void Page_Load(object sender, EventArgs e)
{
chooseImage();
}
public string chooseImage()
{
if (Session["img"] == null)
{
string imgPath;
int fileCount = Directory.GetFiles(Server.MapPath("Slider/"), "*.*", SearchOption.TopDirectoryOnly).Length;
fileCount = fileCount + 1;
imgPath = "Slider/" + RandomNumber(1, fileCount) + ".jpg";
Session["img"] = imgPath;
return imgPath;
}
else
return Session["img"].ToString();
}
private int RandomNumber(int min, int max)
{
Random random = new Random();
return random.Next(min, max);
}
When I run the application no image is being displayed in the slider.
Slider Folder