+1-888-365-2779
Try Now
More in this section

Forums / Sitefinity SDK / Random Image on Load server control

Random Image on Load server control

9 posts, 0 answered
  1. John Tolar
    John Tolar avatar
    87 posts
    Registered:
    07 May 2003
    09 Feb 2011
    Link to this post
    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" />
  2. John Tolar
    John Tolar avatar
    87 posts
    Registered:
    07 May 2003
    11 Feb 2011
    Link to this post
    Nevermind... I had caching turned on the page. Once I turned this off things work correctly!!
  3. Jeff
    Jeff avatar
    118 posts
    Registered:
    08 Jul 2011
    23 Mar 2012
    Link to this post
    Hi John,

    Would you be willing to sare your control?
  4. Grace Hallwachs
    Grace Hallwachs avatar
    144 posts
    Registered:
    03 Nov 2014
    29 Mar 2012
    Link to this post
    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 >>

  5. John Tolar
    John Tolar avatar
    87 posts
    Registered:
    07 May 2003
    24 Aug 2012
    Link to this post
    Jeff, Sorry I haven't seen this message before. If you are still interested I can send you the code.
  6. TMatt
    TMatt avatar
    104 posts
    Registered:
    06 Feb 2011
    26 Aug 2012
    Link to this post
    I´am interessted too in the Code for the control.

    Thanks
  7. John Tolar
    John Tolar avatar
    87 posts
    Registered:
    07 May 2003
    27 Aug 2012
    Link to this post
    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.
  8. TMatt
    TMatt avatar
    104 posts
    Registered:
    06 Feb 2011
    27 Aug 2012
    Link to this post
    Thanks,

    wait for it in Marktplace.
  9. TMatt
    TMatt avatar
    104 posts
    Registered:
    06 Feb 2011
    20 Sep 2012
    Link to this post
    It is inside now ?
9 posts, 0 answered