• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 3367
  • Last Modified:

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

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
DanSmir
Asked:
DanSmir
  • 11
  • 9
  • 3
2 Solutions
 
spprivateCommented:
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
 
DanSmirAuthor Commented:
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
 
spprivateCommented:
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
DanSmirAuthor Commented:
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
 
spprivateCommented:
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
 
spprivateCommented:
I mean you have to use two panels and two different timers
0
 
DanSmirAuthor Commented:
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
 
spprivateCommented:
paste the Code behind please
0
 
DanSmirAuthor Commented:
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
 
spprivateCommented:
hmm
definetly i see difference in the timing.but not as i expected though.It is in its will.
Let me see
0
 
spprivateCommented:
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
 
DanSmirAuthor Commented:
spprivate:


ok, 10x waiting.
0
 
spprivateCommented:
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
 
DanSmirAuthor Commented:
Is it possible to solve it????
0
 
Shaun KlineLead Software EngineerCommented:
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
 
DanSmirAuthor Commented:
Shaun_Kline;

Hey, can you show me please base on my code?
0
 
spprivateCommented:
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
 
DanSmirAuthor Commented:
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
 
Shaun KlineLead Software EngineerCommented:
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
 
Shaun KlineLead Software EngineerCommented:
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
 
spprivateCommented:
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
 
spprivateCommented:
The example i sent is what Shaun talking about.Bingo(Javascript)
0
 
DanSmirAuthor Commented:
Thank you.
0

Featured Post

Fill in the form and get your FREE NFR key NOW!

Veeam is happy to provide a FREE NFR server license to certified engineers, trainers, and bloggers.  It allows for the non‑production use of Veeam Agent for Microsoft Windows. This license is valid for five workstations and two servers.

  • 11
  • 9
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now