Random pictures in ASP (C#)

Posted on 2009-05-20
Medium Priority
Last Modified: 2013-11-07
Hello experts,
I have a random picture view generator that making me a problem.

When i'm adding <asp:Image runat="server" ID="img1" /> it showing me exactly same picture as previous.
Question: How to show different picture by each ID in same page...

Code attached.

Many 10x
protected void Page_Load(object sender, EventArgs e)
    private void SetNextImage()
        string path = Server.MapPath("~/Images/Promo/");
        string[] files = Directory.GetFiles(path);
        Random r = new Random();
        FileInfo fi = new FileInfo(files[r.Next(0, files.Length)]);
        img.ImageUrl = "~/Images/Promo/" + fi.Name;
        img1.ImageUrl = "~/Images/Promo/" + fi.Name;
  <asp:ScriptManager ID="ScriptManager1" runat="server" >
        <asp:UpdatePanel ID="UpdatePanel1" runat="server">
                <asp:Timer ID="Timer1" runat="server" 
                <asp:Image runat="server" ID="img" />
                <asp:Image runat="server" ID="img1" />

Open in new window

Question by:DanSmir
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
LVL 12

Expert Comment

ID: 24430097
I think you have missed the timer.tick event out so its not updating based on the timer.
try this code:

		<asp:Timer ID="Timer1" runat="server" interval="5000" ontick="Timer1_Tick">
// code behind:
	protected void Timer1_Tick(object sender, EventArgs e)

Open in new window


Author Comment

ID: 24430145

But my problem is not a timer...

I need to show different FileInfo fi = new FileInfo(files[r.Next(0, files.Length)]); by ID...

Accepted Solution

demi-os earned 2000 total points
ID: 24430152
if i understood you correctly the bellow code is what you are asking for
    private void SetNextImage()
        string path = Server.MapPath("~/Images/Promo/");
        string[] files = Directory.GetFiles(path);
        Random r = new Random();
        FileInfo fi1 = new FileInfo(files[r.Next(0, files.Length)]);
        FileInfo fi2 = new FileInfo(files[r.Next(0, files.Length)]);
        img.ImageUrl = "~/Images/Promo/" + fi1.Name;
        img1.ImageUrl = "~/Images/Promo/" + fi2.Name;

Open in new window


Author Closing Comment

ID: 31583421

any many 10x. It works!!!!!!

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Suggested Courses

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question