Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > Listbox.SelectedIndex is often 0, when it shouldn't be

Reply
Thread Tools

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

 
 
JRH JRH is offline
Junior Member
Join Date: Jul 2007
Posts: 1
 
      07-05-2007
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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Adobe crashes in F.F. way TOO OFTEN Gypsy Firefox 5 12-31-2005 01:46 PM
MCSE: Get Laid More Often? RoryPhlapp MCSE 17 07-29-2004 02:35 PM
OT: sometimes is sucks to be MS (but not often) Neil MCSE 2 07-05-2004 11:21 PM
Show 4 Records that occur more often...and how often they occur. Miguel Dias Moura ASP .Net 4 05-06-2004 03:40 PM
LWP::Authen::Ntlm Error when often used Reto Zingg Perl 0 09-28-2003 09:56 PM



Advertisments