Listbox.SelectedIndex is often 0, when it shouldn't be

Discussion in '.NET' started by JRH, Jul 5, 2007.

  1. JRH

    JRH

    Joined:
    Jul 5, 2007
    Messages:
    1
    I have a page with a few controls, add and remove buttons, a text entry box, and a listbox. The listbox gets entries from a database:

    Code:
    protected void Page_Load(object sender, EventArgs e)
        {
            source = new DetailsDataSource();
            if (!Page.IsPostBack)
            {
                //Add companies to the list
                companies = DetailsDataSource.getByDetailNameandId(new int[] { 0 }, "company_name");
                ArrayList CompanyList = new ArrayList();
                foreach (DetailsObject detail in companies)
                {
                    CompanyList.Add(detail.DetailValue);
                    //lstCompaniesAdded.Items.Add(new ListItem(detail.DetailValue, detail.DetailName));
                }
                
                lstCompaniesAdded.DataSource = CompanyList;
                lstCompaniesAdded.DataBind();
            }        
        }
    
    I add items and remove them when the buttons are clicked.
    Code:
    protected void AddCompany(object sender, ImageClickEventArgs e)
        {
            if (companyDetail.Text == "")
            {
                EntryError.Text = "Please type in detail information.";
                EntryError.Visible = true;
                return;
            }
    
            DetailsObject entry = new DetailsObject();
            entry.DetailValue = companyDetail.Text;
            entry.DetailName = "company_name";
            entry.SigID = 0;
            if (source.Insert(entry))
            {
                // Move the detail from the entry box to the read-only box
                lstCompaniesAdded.Items.Add(new ListItem(entry.DetailValue, entry.DetailName));
                companyDetail.Text = "";
                EntryError.Visible = false;
            }
            else
            {
                EntryError.Text = "That entry already exists.";
                EntryError.Visible = true;
            }
        }
    
        protected void RemoveCompany(object sender, ImageClickEventArgs e)
        {
            ArrayList list = new ArrayList();
            list.AddRange(lstCompaniesAdded.GetSelectedIndices());
            if (lstCompaniesAdded.SelectedIndex > -1)
            {
                DetailsObject entry = new DetailsObject();
    
                int total = lstCompaniesAdded.Items.Count;
                for (int i = 0; i < total; i++)
                {
                    if (list.Contains(i))
                    {
                        list.Remove(i);
                        entry.DetailValue = lstCompaniesAdded.Items[i].Text;
                        entry.DetailName = "company_name";
                        entry.SigID = 0;
                        if (source.Delete(entry.DetailValue, entry.DetailName))
                        {
                            lstCompaniesAdded.Items.RemoveAt(i);
                            i--;
                            total--;
                        }
                        else
                        {
                            this.EntryError.Text = "That entry could not be found.";
                            this.EntryError.Visible = true;
                        }
                    }
                }
                
            }
        }
    Also, here is the listbox (I did the enableviewstate thing because one other person's solution said to, but it hasn't changed anything.):
    <asp:ListBox EnableViewState="true" ID="lstCompaniesAdded" runat="server" Width="459" Height="350" ReadOnly="true" ></asp:ListBox>

    The problem isn't really in remove, it doesn't matter where I try to grab the index. I currently have the listbox set to only allow single selections. It works fine when nothing is added to the list before deleting. If the page loads and there are four items, the selected index is always correct. If I start adding values, then the index will be the oldest item added. For example:

    List at first: 0 1 2 3
    I add 4 and 5: 0 1 2 3 4 5
    I select 5 and hit delete-> 0 1 2 3 5 four is gone.
    Next it will delete 5, no matter what I pick.

    If the list was empty, and then I added values, it will always delete the oldest.

    I've seen a lot of people with similar problems, but I have yet to find a solution that works for me too.

    Thank you very much.
    JRH, Jul 5, 2007
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. John Giblin

    Asp:ListBox and selectedindex

    John Giblin, Nov 26, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    1,097
    John Giblin
    Nov 26, 2003
  2. S. Justin Gengo

    ListBox SelectedIndex is always -1

    S. Justin Gengo, Feb 17, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    2,755
  3. Miguel Dias Moura
    Replies:
    4
    Views:
    444
    Hans Kesting
    May 6, 2004
  4. =?Utf-8?B?TGll?=
    Replies:
    5
    Views:
    673
    =?Utf-8?B?TGll?=
    Dec 14, 2004
  5. =?Utf-8?B?Q2FybG8gTWFyY2hlc29uaQ==?=

    ListBox not returning correct SelectedIndex

    =?Utf-8?B?Q2FybG8gTWFyY2hlc29uaQ==?=, Feb 4, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    4,266
    Karl Seguin
    Feb 4, 2005
Loading...

Share This Page