Solved

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

Posted on 2009-05-20
23
3,259 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
ID: 24432220
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
ID: 24432279
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
ID: 24432769
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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 

Author Comment

by:DanSmir
ID: 24432859
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
ID: 24433111
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
ID: 24433118
I mean you have to use two panels and two different timers
0
 

Author Comment

by:DanSmir
ID: 24433188
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
ID: 24433233
paste the Code behind please
0
 

Author Comment

by:DanSmir
ID: 24433257
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
ID: 24433291
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
ID: 24433337
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
 

Author Comment

by:DanSmir
ID: 24433397
spprivate:


ok, 10x waiting.
0
 
LVL 15

Expert Comment

by:spprivate
ID: 24433426
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
ID: 24433442
Is it possible to solve it????
0
 
LVL 26

Expert Comment

by:Shaun Kline
ID: 24434003
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
ID: 24434313
Shaun_Kline;

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

Expert Comment

by:spprivate
ID: 24434493
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
ID: 24434528
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 26

Expert Comment

by:Shaun Kline
ID: 24434739
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 26

Assisted Solution

by:Shaun Kline
Shaun Kline earned 100 total points
ID: 24434795
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
ID: 24434823
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
ID: 24434832
The example i sent is what Shaun talking about.Bingo(Javascript)
0
 

Author Closing Comment

by:DanSmir
ID: 31583532
Thank you.
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
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…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

803 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