Improve company productivity with a Business Account.Sign Up

x
?
Solved

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

Posted on 2009-05-20
23
Medium Priority
?
3,400 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
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

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 27

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 27

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 27

Assisted Solution

by:Shaun Kline
Shaun Kline earned 300 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 450 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

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
In real business world data are crucial and sometimes data are shared among different information systems. Hence, an agreeable file transfer protocol need to be established.
Through the video, you can check the migration process of Outlook PST file to PDF. Kernel for Outlook to PDF tool can convert Outlook emails with all attributes like Subject, To, From, Cc, Bcc and other folders such as Inbox, Outbox, Sent Items, Jun…
Watch the video to know the process of migration of Exchange or Office 365 mailboxes in absence of MS Outlook. It is an eminent tool which can easily migrate Public, Archive user mailboxes from one another Exchange server and Office 365. Kernel Migr…

595 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