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

JRH

Joined
Jul 5, 2007
Messages
1
Reaction score
0
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top