Solved

Random pictures in ASP (C#)

Posted on 2009-05-20
4
745 Views
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
aspx.cs
 

protected void Page_Load(object sender, EventArgs e)

    {

        SetNextImage();

    }

    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;

    }
 
 

aspx
 

  <asp:ScriptManager ID="ScriptManager1" runat="server" >

        </asp:ScriptManager>

        <asp:UpdatePanel ID="UpdatePanel1" runat="server">

            <ContentTemplate>

                <asp:Timer ID="Timer1" runat="server" 

                     Interval="5000">

                </asp:Timer>

                <asp:Image runat="server" ID="img" />

                <asp:Image runat="server" ID="img1" />

            </ContentTemplate>

        </asp:UpdatePanel>

Open in new window

0
Comment
Question by:DanSmir
  • 2
4 Comments
 
LVL 12

Expert Comment

by:GuitarRich
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">

		</asp:Timer>
 

// code behind:

	protected void Timer1_Tick(object sender, EventArgs e)

	{

		SetNextImage();

	}

Open in new window

0
 

Author Comment

by:DanSmir
ID: 24430145
GuitarRich:

10x,
But my problem is not a timer...

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

Accepted Solution

by:
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

0
 

Author Closing Comment

by:DanSmir
ID: 31583421
demi-os

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

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

708 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