Random Image on Load server control - Front- & Back-End Development - Front- & Back-End Development - Progress Community
 Front- & Back-End Development

Random Image on Load server control

  • Random Image on Load server control
  • 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" />
  • Nevermind... I had caching turned on the page. Once I turned this off things work correctly!!
  • Hi John,

    Would you be willing to sare your control?
  • 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 >>
  • Jeff, Sorry I haven't seen this message before. If you are still interested I can send you the code.
  • I´am interessted too in the Code for the control.

    Thanks
  • 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.
  • Thanks,

    wait for it in Marktplace.
  • It is inside now ?