i put a list box and an image box.
now i want the image to swap every time the user clicks on a different element in the list. it doesnt seem to work :(

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page 
{
    string[] pictures = { "~/createWii.jpg", 
                               "~/DKC4_wii.png",
                                "~/Donkey-Kong-Country-1.jpg", 
                                "~/DSCallOfDutyBlackOps.jpg", 
                                "~/DSPreviewsCodmw2.jpg", 
                                "~/DSPreviewsAliceInWonderLAnds.jpg",
                               "~/DSPreviewPicross3d.jpg",
                               "~/createii.jpg", 
                               
                           
                            
                           };
    string[] picturesNames = { "picture1", "picture2", "picture3", "picture4", "picture5", "picture6", "picture7", "picture8" };
 
    protected void Page_Load(object sender, EventArgs e)
    {
        for (int i = 0; i < pictures.Length; i++)
	    {
                ListBox1.Items.Add(new ListItem(picturesNames[i],pictures[i]));
         
	    }
    
        Image1.ImageUrl = "~/Donkey-Kong-Country-1.jpg";
        

        ListBox1.DataSource = picturesNames;
        ListBox1.DataBind();
    }
    protected void ListBox1_SelectedIndexChanged(object sender, EventArgs e)
    {
       
   

        Image1.ImageUrl = pictures[ListBox1.SelectedIndex];// it tells me that there is index out of range each time. why ?
  
       
        
    }
}

Server Error in '/WebSite1' Application.
Index was outside the bounds of the array.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.IndexOutOfRangeException: Index was outside the bounds of the array.

Source Error:

Line 41:
Line 42:
Line 43: Image1.ImageUrl = pictures[ListBox1.SelectedIndex];
Line 44:
Line 45:

Try posting this in the ASP.Net forum

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.