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

  • Home
  • SEARCH
  • 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 96971
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T23:54:56+00:00 2026-05-10T23:54:56+00:00

I have a list box control: <asp:ListBox runat=server id=lbox autoPostBack=true /> The code behind

  • 0

I have a list box control:

  <asp:ListBox runat='server' id='lbox' autoPostBack='true' />  

The code behind resembles:

  private void Page_Load(object sender, System.EventArgs e) {     lbox.SelectedIndexChanged+=new EventHandler(lbox_SelectedIndexChanged);     if(!Page.IsPostBack)     {         LoadData();          } } private LoadData() {     lbox.DataSource = foo();     lbox.DataBind(); } protected void lboxScorecard_SelectedIndexChanged(object sender, EventArgs e) {     int index = (sender as ListBox).selectedIndex; }  

My problem is that when my page receives a post back (when a user makes a selection in the listbox), the selection always ‘jumps’ to the first item in the listbox, so that the index variable in my callback function is always 0.

Seems like this may be a viewstate problem? How can I fix it so that the selection index remains through the postback?

There is no ajax going on, this is .NET 1.0.

Thanks.

EDIT 1 JohnIdol has gotten me a step closer, If I switch the datasource from my original DataTable to an ArrayList, then everything work properly…what would cause this?

Edit 2 It turns out that my DataTable had multiple values that were the same, so that the indexes were treated as the same as all items with the same value…thanks to those who helped!

  • 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. 2026-05-10T23:54:56+00:00Added an answer on May 10, 2026 at 11:54 pm

    What’s the output of the foo() function call?

    Populating manually the list box you can set indexes to whatever you want (all 0 for example) – so the same thing can happen setting a given dataSource under certain circumstances (one that specifies indexes I suppose). If all the item indexes are 0 the result is that the SelectedIndexChanged event is not raised (index does not change!) and everything is messed up: on post-back selection will go back to the first item in the list.

    This would explain it – I cannot think of anything else – it is working fine for me on .NET 2.0 I am using an ArrayList with strings to populate the listBox.

    The only way I can reproduce your issue is setting all indexes to 0.

    I’d say add a watch to the ListBox and check the indexes at runtime to make sure they’re not all zeroes.

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

Sidebar

Ask A Question

Stats

  • Questions 64k
  • Answers 65k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer The development server by default doesn't serve static files (see… May 11, 2026 at 11:00 am
  • added an answer You shouldn't need to make a partial class. What does… May 11, 2026 at 11:00 am
  • added an answer There is no BeforeServiceStart, but you could try Before='StartServices'. May 11, 2026 at 11:00 am

Related Questions

I have a list box control: <asp:ListBox runat=server id=lbox autoPostBack=true /> The code behind
I have a ListBox control that I want to change into having a toggle
I have a composite control that contains a ListBox child control. On postback, the
I have a function that presents the user a combo-box. def select_interface(interfaces) list_box :items
I have a asp:ListBox wrapped inside an asp:UpdatePanel. The list box contains all the
I have a ListBox that has a style defined for ListBoxItems. Inside this style,
I have a listbox where the items contain checkboxes: <ListBox Style={StaticResource CheckBoxListStyle} Name=EditListBox> <ListBox.ItemTemplate>
I have a ListBox which displays items of variable height. I want to show
I have a ListBox with a bunch of images in it (done through a
I have a listbox that is databound to a Collection of objects. The listbox

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.