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

How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

Question has a verified solution.

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

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

831 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