Random pictures in ASP (C#)

Posted on 2009-05-20
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
  • 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 500 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …
As a trusted technology advisor to your customers you are likely getting the daily question of, ‘should I put this in the cloud?’ As customer demands for cloud services increases, companies will see a shift from traditional buying patterns to new…

867 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now