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 3955156
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T02:11:40+00:00 2026-05-20T02:11:40+00:00

I put a list box and an image box. now I want the image

  • 0

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 ?
}

}

  • 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-20T02:11:41+00:00Added an answer on May 20, 2026 at 2:11 am

    A couple of things.

    1.) you should wrap the code in page_load with

    if(!IsPostback)
    

    2.) Make sure on the .aspx that the “AutoPostback” property is set to true on the listbox!

    Edit

    Per the request in the comments, the reason this is needed is two fold.

    1. ASP.NET ViewState will handle the persistence of the values on postback, therefore, you can use the !IsPostback condition to ensure that the information is only bound once. This prevents any “oddities” from coming up in the future.

    2. By default ListBoxes/DropDownLists/etc do not post back automatically when the user changes a selection. So to actually trigger the event you either need to have a button that does the postback, or update the “AutoPostback” property as I directed to ensure that when the user makes a change that it triggers the server-side code.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am wondering why I can't put a list of text box in parameter
Hello everyone I've been trying to put some objects in an ASP.NET list box
I want to get all the objects from the list and put them into
I search for a good and handy user interface to put items from list
I want to put these strings in a list, how do I do that?
I want to put an encrypted license list on the web, so that valid
I wanted to put a background image partially visible behind a list view. I
I am trying to take text from a list box control and put it
Is it possible to put a combo box inside a list box? I'd like
I have a list box in which there are around say 10 items. Now

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.