Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 939123
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T21:44:24+00:00 2026-05-15T21:44:24+00:00

I am trying to figure out how to do a server ASP.NET MVC Image

  • 0

I am trying to figure out how to do a server ASP.NET MVC Image Load on Refresh. Basically, I have an array of image’s I would like to rotate on refresh on the page. I know how to do this in jQuery but the problem is that all images load at the same time [which sucks in terms of performance]

Does anyone know how I could do this ?

Currently JS looks like below but I really want it server-side:

function random_imglink(){
  var myimages=new Array()
  //specify random images below. You can have as many as you wish
  myimages[1]="img1.gif"
  myimages[2]="img2.gif"
  myimages[3]="img3.gif"

  //specify corresponding links below
  var imagelinks=new Array()
  imagelinks[1]="http://www.example1.com/"
  imagelinks[2]="http://www.example2.com"
  imagelinks[3]="http://www.example3.com"

  var ry=Math.floor(Math.random()*myimages.length)

  if (ry==0)
     ry=1
     document.write('<a href='+'"'+imagelinks[ry]+'"'+'><img src="'+myimages[ry]+'" border=0></a>')
}

  random_imglink()

Thx

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T21:44:25+00:00Added an answer on May 15, 2026 at 9:44 pm

    How about writing a helper:

    private static Random _random = new Random();
    public static MvcHtmlString RandomImage(this HtmlHelper htmlHelper)
    {
        var img = new TagBuilder("img");
        var urlHelper = new UrlHelper(htmlHelper.ViewContext.RequestContext);
        var number = _random.Next(1, 4);
        var src = urlHelper.Content(string.Format("~/img{0}.gif", number));
        img.Attributes["src"] = src;
        img.Attributes["border"] = "0";
        return MvcHtmlString.Create(img.ToString());
    }
    

    Which you could use in your page:

    <%= Html.RandomImage() %>
    

    And if you want to also generate an anchor:

    public static class HtmlExtensions
    {
        private class Image
        {
            public string Src { get; set; }
            public string Href { get; set; }
        }
    
        private static Random _random = new Random();
        private static Image[] _images = new[]
        {
            new Image { Src = "img1.gif", Href = "http://www.example1.com" },
            new Image { Src = "img2.gif", Href = "http://www.example2.com" },
            new Image { Src = "img3.gif", Href = "http://www.example3.com" }
        };
    
        public static MvcHtmlString RandomImage(this HtmlHelper htmlHelper)
        {
            var anchorTag = new TagBuilder("a");
            var randomImage = _images[_random.Next(0, _images.Length)];
            anchorTag.Attributes["href"] = randomImage.Href;
    
            var imageTag = new TagBuilder("img");
            imageTag.Attributes["src"] = randomImage.Src;
            imageTag.Attributes["border"] = "0";
            anchorTag.InnerHtml = imageTag.ToString();
            return MvcHtmlString.Create(anchorTag.ToString());
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to figure out the membership module in asp.net. Currently i have a
I'm teaching myself ASP.NET MVC, and am trying to figure out where best to
I'm trying to figure out what is more efficient in terms of server load
Learning how to do a master page in asp.net. Trying to figure out how
General Info: ASP.NET MVC app using ADO.NET Entity Framework SQL Server 2005 I have
New to VB.net, just trying to figure out how to properly manipulate the asp:Label
I am trying to figure out the efficiency of my server side code. Using
I'm trying to figure out a way to use tar+pipes on a Ubuntu Server
I'm working on a client-server Android application and trying to figure out how to
In SQL Server 2005, I'm trying to figure out why I'm not able to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.