Random Image on Load server control

Posted by Community Admin on 05-Aug-2018 23:48

Random Image on Load server control

All Replies

Posted by Community Admin on 09-Feb-2011 00:00

I have created a server control to allow the user to select a image library in which to display a random image on page load. 

They upload several images on into a image library and then drop the "Static Image Rotator" server control widget onto the page. The purpose is to show a random image from this library as the main image on the home page. 

I have the code working, the problem is it appears that the page designer copies the created html to the database and displays that content when the page is viewed. This causes the image to be random when dropped on the page in design mode, but static (doesn't choose a new random image) on the live page.

In the control I am using 

protected override void InitializeControls(GenericContainer container)
        
            _uniqueId = RandomNumber(3000, 32000);
            ImgRotator.CssClass = string.Format("ImgRotator 0", _wrapperClass);
            
            try
            
                if (!string.IsNullOrEmpty(_selectedAlbum))
                
                    using (var sf = App.WorkWith())
                    
                        var albm = sf.Album(Guid.Parse(_selectedAlbum)).Get();
                        var images = albm.Items;
                         var rndImageIndex = RandomNumber(0, images.Count() - 1);
 
                        ImgRotator.ImageUrl = images[rndImageIndex].MediaUrl;
                    
                
             catch (Exception ex)
            
                //lblAlbum.Text += ex.Message;
                HttpContext.Current.Response.Write(ex.Message);
            
        

I have also tried the same code on the OnLoad event
protected override void OnLoad(EventArgs e)
        
            base.OnLoad(e);
            ImgRotator.CssClass = string.Format("ImgRotator ", _wrapperClass);
            try
            
                if (!string.IsNullOrEmpty(_selectedAlbum))
                
                    using (var sf = App.WorkWith())
                    
                        var albm = sf.Album(Guid.Parse(_selectedAlbum)).Get();
                        var images = albm.Items;
                        //lblAlbum.Text = "images count " + images.Count();
 
                        var rndImageIndex = RandomNumber(0, images.Count() - 1);
 
                        //lblAlbum.Text = "images[" + rndImageIndex + "]";
 
                        ImgRotator.ImageUrl = images[rndImageIndex].MediaUrl;
 
                        //lblAlbum.Text += " - " + ImgRotator.ImageUrl;
                    
                
            
            catch (Exception ex)
            
                //lblAlbum.Text += ex.Message;
                HttpContext.Current.Response.Write(ex.Message);
            
 
        

I can refresh the page in design mode and get a random image each time, but in live mode (not logged in so no edit tab on the side) I get the same image each time.

The ASCX is very simple.
<%@ Control Language="C#" %>
<asp:Image runat="server" ID="imgRotator" ClientIDMode="Static" />

Posted by Community Admin on 11-Feb-2011 00:00

Nevermind... I had caching turned on the page. Once I turned this off things work correctly!!

Posted by Community Admin on 23-Mar-2012 00:00

Hi John,

Would you be willing to sare your control?

Posted by Community Admin on 29-Mar-2012 00:00

Hello,

Please let us know if you run into anymore issues.

Regards,
Grace Hallwachs
the Telerik team

Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

Posted by Community Admin on 24-Aug-2012 00:00

Jeff, Sorry I haven't seen this message before. If you are still interested I can send you the code.

Posted by Community Admin on 26-Aug-2012 00:00

I´am interessted too in the Code for the control.

Thanks

Posted by Community Admin on 27-Aug-2012 00:00

Due to interest and my upcoming partner status I will release this to the marketplace with full source for free. I am slammed with work right now but should have it up in a week or so. I wrote this during the 4.0 release so it should be backwards compatible for all version of 4.x and 5.x.

Posted by Community Admin on 27-Aug-2012 00:00

Thanks,

wait for it in Marktplace.

Posted by Community Admin on 20-Sep-2012 00:00

It is inside now ?

This thread is closed