Solved

Random pictures in ASP (C#) different asp:Timer

Posted on 2009-05-20
23
3,240 Views
Last Modified: 2013-11-07
Hello experts,
I have  random picture generator that changing picture by using some time interval.
My question is: How it possible to make different time interval for each picture i mean for every ID???....

Code attached..

Thank you guys.
.aspx
 

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

        </asp:ScriptManager>

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

            <ContentTemplate>

                <asp:Timer ID="Timer1" runat="server" Interval="3500">

                </asp:Timer>

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

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

             <a href=""><asp:Image runat="server" ID="img2" /></a>

             <a href=""><asp:Image runat="server" ID="img3" /></a>

             <a href=""><asp:Image runat="server" ID="img4" /></a>

             <a href=""><asp:Image runat="server" ID="img5" /></a>

            </ContentTemplate>

        </asp:UpdatePanel>
 

aspx.cs (code behind)
 

using System;

using System.IO;
 

public partial class _Default : System.Web.UI.Page

{

    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 fi1 = new FileInfo(files[r.Next(0, files.Length)]);

        FileInfo fi2 = new FileInfo(files[r.Next(0, files.Length)]);

        FileInfo fi3 = new FileInfo(files[r.Next(0, files.Length)]);

        FileInfo fi4 = new FileInfo(files[r.Next(0, files.Length)]);

        FileInfo fi5 = new FileInfo(files[r.Next(0, files.Length)]);

        FileInfo fi6 = new FileInfo(files[r.Next(0, files.Length)]);
 

        img.ImageUrl = "~/Images/Promo/" + fi1.Name;

        img1.ImageUrl = "~/Images/Promo/" + fi2.Name;

        img2.ImageUrl = "~/Images/Promo/" + fi3.Name;

        img3.ImageUrl = "~/Images/Promo/" + fi4.Name;

        img4.ImageUrl = "~/Images/Promo/" + fi5.Name;

        img5.ImageUrl = "~/Images/Promo/" + fi6.Name;
 

    }

}

Open in new window

0
Comment
Question by:DanSmir
  • 11
  • 9
  • 3
23 Comments
 
LVL 15

Expert Comment

by:spprivate
Comment Utility
Just a shot.
Name the file names with the intervals you want to see or part of the file name with the duration.
Say myfile1_25.jpg
then reset the timer interval property as soon as this image is loaded.by getting the ticker value from the name
I am trying this code in a sample
0
 

Author Comment

by:DanSmir
Comment Utility
spprivate:

I need different time intervals not for images. I need different intervals for ID's like:

 <a href=""><asp:Image runat="server" ID="img"  /></a>
             <a href=""><asp:Image runat="server" ID="img1" /></a>    interval:1 sec
             <a href=""><asp:Image runat="server" ID="img2" /></a>    interval:2 sec
             <a href=""><asp:Image runat="server" ID="img3" /></a>     interval:3 sec
             <a href=""><asp:Image runat="server" ID="img4" /></a>     interval:4 sec
             <a href=""><asp:Image runat="server" ID="img5" /></a>     interval:5 sec

10x.
0
 
LVL 15

Expert Comment

by:spprivate
Comment Utility
Oh I see what you mean
You show all the images at a time but each control changes images on a speficied time rather than fixed interval.
I guess you need to use different panels.
This is what I did
<form id="form1" runat="server">

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

        <div>

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

        <ContentTemplate>

                <asp:Timer ID="Timer1" runat="server" Interval="5000">

                </asp:Timer>

            

             <a href=""><asp:Image runat="server" ID="img5" /></a>

            </ContentTemplate>
 

        </asp:UpdatePanel>

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

        <ContentTemplate>

                <asp:Timer ID="Timer2" runat="server" Interval="2000">

                </asp:Timer>

            

             <a href=""><asp:Image runat="server" ID="Image1" /></a>

            </ContentTemplate>
 

        </asp:UpdatePanel>

        </div>

    </form>
 
 

Code Behind
 

 protected void Page_Load(object sender, EventArgs e)

    {

        SetNextImage();

    }

    private void SetNextImage()

    {

        string path = Server.MapPath("~/Images/");

        string[] files = Directory.GetFiles(path);

        Random r = new Random();
 

        FileInfo fi5 = new FileInfo(files[r.Next(0, files.Length)]);

        FileInfo fi6 = new FileInfo(files[r.Next(0, files.Length)]);

        if (fi6.Name != "Thumbs.db") { img5.ImageUrl = "~/Images/" + fi6.Name; }

        if (fi5.Name != "Thumbs.db") { Image1.ImageUrl = "~/Images/" + fi5.Name; }

        

        

    }

Open in new window

0
 

Author Comment

by:DanSmir
Comment Utility
spprivate:

I tried your code but it still changing ALL ID's in same time... BTW it using Timer2 for bot of id's

Waiting for answer,

Dan
0
 
LVL 15

Expert Comment

by:spprivate
Comment Utility
Did you change the time interval to different times Look in my code
I have given timer1
asp:Timer ID="Timer1" runat="server" Interval="5000">
timer 2
<asp:Timer ID="Timer2" runat="server" Interval="2000">

it works perfectly

Also make sure the images are in two different update panels
0
 
LVL 15

Expert Comment

by:spprivate
Comment Utility
I mean you have to use two panels and two different timers
0
 

Author Comment

by:DanSmir
Comment Utility
spprivate:

I did it,
unsuccessful. It still changing all id's by the lowest interval value, without getting value from TimerID.
I tried to change time value in both timers, same trouble it changing all id's by small one,

<asp:ScriptManager ID="ScriptManager1" runat="server" />
        <div>
        <asp:UpdatePanel ID="UpdatePanel1" runat="server">
        <ContentTemplate>
                <asp:Timer ID="Timer1" runat="server" Interval="1000">
                </asp:Timer>
           
             <a href=""><asp:Image runat="server" ID="img5" /></a>
            </ContentTemplate>
 
        </asp:UpdatePanel>
        <asp:UpdatePanel ID="UpdatePanel2" runat="server">
        <ContentTemplate>
                <asp:Timer ID="Timer2" runat="server" Interval="50000">
                </asp:Timer>
           
             <a href=""><asp:Image runat="server" ID="Image1" /></a>
            </ContentTemplate>
 
        </asp:UpdatePanel>
0
 
LVL 15

Expert Comment

by:spprivate
Comment Utility
paste the Code behind please
0
 

Author Comment

by:DanSmir
Comment Utility
Exact as you sent me:

protected void Page_Load(object sender, EventArgs e)
    {
        SetNextImage();
    }
    private void SetNextImage()
    {
        string path = Server.MapPath("~/Images/");
        string[] files = Directory.GetFiles(path);
        Random r = new Random();
 
        FileInfo fi5 = new FileInfo(files[r.Next(0, files.Length)]);
        FileInfo fi6 = new FileInfo(files[r.Next(0, files.Length)]);
        if (fi6.Name != "Thumbs.db") { img5.ImageUrl = "~/Images/" + fi6.Name; }
        if (fi5.Name != "Thumbs.db") { Image1.ImageUrl = "~/Images/" + fi5.Name; }
       
       
    }
0
 
LVL 15

Expert Comment

by:spprivate
Comment Utility
hmm
definetly i see difference in the timing.but not as i expected though.It is in its will.
Let me see
0
 
LVL 15

Expert Comment

by:spprivate
Comment Utility
I see where the problem is.
Because every time a postback is triggered, your update panel reloads and changes the images
So the shorter time is messing it up.
What happens here is setnextimage is called every time in a post back right.
That changes images in both ids.Should have thought about it.
let me see
0
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 

Author Comment

by:DanSmir
Comment Utility
spprivate:


ok, 10x waiting.
0
 
LVL 15

Expert Comment

by:spprivate
Comment Utility
This turn out to be tricky.
Every time when page post backs (timer tick) it resets the tick to original values.
So you always see lowest timer event :)
0
 

Author Comment

by:DanSmir
Comment Utility
Is it possible to solve it????
0
 
LVL 25

Expert Comment

by:Shaun Kline
Comment Utility
If you not set on using a single page, multiple iframes on your main page with each iframe pointing to a page that basically loads the random image may work.
0
 

Author Comment

by:DanSmir
Comment Utility
Shaun_Kline;

Hey, can you show me please base on my code?
0
 
LVL 15

Expert Comment

by:spprivate
Comment Utility
What shaun say is have say 2 aspx pages and then load them in iframes in a master page.
That way each page will be of its own

Just like html frames
0
 

Author Comment

by:DanSmir
Comment Utility
Guys,
1st Thank you for help.
But i think that must be another way to solve my problem.

Does anyone know other way to load randomly 5 divs or id's with images inside and make a rotation separated for each div or id?
0
 
LVL 25

Expert Comment

by:Shaun Kline
Comment Utility
There is the possibility (i.e. I've never tried it) of using straight Javascript and the setInterval function to change the source of your image. Depending on how large your file list will be, you could load the file path for all of your images into a javascript array, randomly select an item in the array, and then set the image source to the file path stored in that item.

An alternative to loading the file list into a javascript array would be to call a web method to perform the random selection of the file path.
0
 
LVL 25

Assisted Solution

by:Shaun Kline
Shaun Kline earned 100 total points
Comment Utility
I just found this article: http://www.asp.net/AJAX/Documentation/Live/tutorials/TimerControlWithUpdatePanelsTutorial.aspx which may work using multiple timers. The main addition is using an AsyncPostbackTrigger for each UpdatePanel for the Timer's Tick event.
0
 
LVL 15

Accepted Solution

by:
spprivate earned 150 total points
Comment Utility
You can do magic with javascript.
Attahched is a sample html with all needed images and javascripts .The sample is in html.
You can implement same functioanlity in asp.net using those js files.I can send you that zipfile in email since ee doesnt allow me to upload jscript files.
Or you can do it from this site where I downloaded.

http://www.dyn-web.com/code/rotate_images/
0
 
LVL 15

Expert Comment

by:spprivate
Comment Utility
The example i sent is what Shaun talking about.Bingo(Javascript)
0
 

Author Closing Comment

by:DanSmir
Comment Utility
Thank you.
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

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…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
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…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

743 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

15 Experts available now in Live!

Get 1:1 Help Now