I created an image element in web page. I am extracting image urls from database and wanted to set the url of the image element to these values with a gap of 5 seconds of interval each.
How can I achieve this?
here is my sample code…
RollingScreenBAL bal = new RollingScreenBAL();
DetailsForSlideShow[] details = bal.GetDetailsForSlideShow(username);
foreach (DetailsForSlideShow detail in details)
{
imgSlideShow.Attributes["src"] = ResolveUrl(detail.GraphUrl);
Thread.Sleep(detail.TimeInterval);
}
By the above code I am able to set the image url only for the first value that i am getting from database….
What you are describing seems much like a client-server with “push logic” from server using an extabilished, permanent communication. This is not possible to get working on the web. Last, using sleep events during a page processing, it’s not always a good idea, unless there are some specific reasons (i.e. to wait a specific resource to get available). Even when using an ajax update panel it’s always the client to trigger the event, leading to server.
To have your slideshow, you have many options, I will show you two of them, but you can also use ready to use plugins, there are tons of examples on the web, just search for them.
1- A quick and dirty solution, but hopefully for a short number of images. You can create a javascript array based on db content, and use it as if they were statically created for a simple slideshow javascript, and let it change through SetInterval js function.
c# code:
web page:
This will render to web page to something like:
Which you can later use/cycle with javascript:
and then start the slideshow, with a script at end of html (or on onload event in body)
note: you can generate/append also this script in c# RegisterClientScriptBlock as shown before, just change the “arraykey” value every time. More infos here: http://msdn.microsoft.com/it-it/library/bahh2fef%28v=vs.80%29.aspx
2- A better solution is to use jquery’s ajax method to get “next image” from a c# web method.
web page:
javascript code:
cs code:
note: if you are missing what jquery is, search the web for it, I’m sure it will help you a lot using javascript.