Explanation:
1. Add an Image Tag to your.
<asp:Image ID="Image1" runat="server"/>
2. Create a Collection of your Images using string Array.
string[] images = new string[] { "/images/Backcolor.gif", "/images/Bold.gif",
"/images/cut.gif" };
3. Use System.Random object
to get a Rendom Image path.
System.Random r = new Random();
string returningImage=images[r.Next(0,images.Length)];
4. Create a Url from the given path:
string path = Request.ApplicationPath + GetRendomImage();
5. Assign the return path to your Image Tag.
Image1.ImageUrl = path;
Image1.AlternateText = path;
Now, the Complete code behind will look like this:
protected void Page_Load(object sender, EventArgs e)
{
string path = Request.ApplicationPath + GetRendomImage();
Image1.ImageUrl = path;
Image1.AlternateText = path;
}
protected string GetRendomImage()
{
System.Random r = new Random();
string[] images = new string[] { "/images/Backcolor.gif", "/images/Bold.gif",
"/images/cut.gif" };
return images[r.Next(0,images.Length)];
}
|