Wrox Programmer Forums
Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 2.0 > ASP.NET 2.0 Basics
|
ASP.NET 2.0 Basics If you are new to ASP or ASP.NET programming with version 2.0, this is the forum to begin asking questions. Please also see the Visual Web Developer 2005 forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 2.0 Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old April 10th, 2007, 03:12 PM
Registered User
 
Join Date: Apr 2007
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default ListBox not showing correct selected value

Hi,
      I have a pretty annoying problem with one of my pages listboxes. I want to take the text of the selected item in this particular multi select listbox and put the selected accounts in an arraylist. However when I submit the form, the selected index is correct sometimes and sometimes it comes to the first item even if I have something else selected. I read somewhere this could happen if I was binding it twice, but I am only binding it once, in an event handler of a Drop down control, I am not binding it in the page load and I tried using Listbox.SelectedIndex = -1 but it still does not work, oh and the EnableViewstate is also set to True for the Listbox. Its very frustrating as I cannot seem to find a way to get around it, could someone please tell me what I need to do to get my correct selected item in the listbox???

here is the code

// The event handler that populates the Listbox LbAccounts

protected void DDLSignatories_SelectedIndexChanged(object sender, EventArgs e)
    {
        GetAccounts();
    }


 protected void GetAccounts()
    {

        System.Data.SqlClient.SqlConnection cnn = new System.Data.SqlClient.SqlConnection(cnnString);
        SqlCommand cmd = new SqlCommand("sp_GetEntityAcctstoRemoveSigs", cnn);
        cmd.Parameters.AddWithValue("@EID", DDLEntities.SelectedValue);
        cmd.Parameters.AddWithValue("@SigID", DDLSignatories.SelectedValue);
        cmd.CommandType = CommandType.StoredProcedure;
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataTable Accounts = new DataTable();
        da.Fill(Accounts);
        if (Accounts.Rows.Count == 0)
        {
            Acct.Style["display"] = "none";
            lblNoAccounts.Visible = true;
            return;
        }
        lblNoAccounts.Visible = false;
        Acct.Style["display"] = "block";
        LbAccounts.DataSource = Accounts;
        LbAccounts.DataTextField = "Name";
        LbAccounts.DataValueField = "Type_ID";
        LbAccounts.DataBind();
        LbAccounts.SelectedIndex = -1;
    }

// and Finally the Submit button

 protected void BtnSubmit_Click(object sender, EventArgs e)
    {
        System.Data.SqlClient.SqlConnection cnn = new System.Data.SqlClient.SqlConnection(cnnString);

        ArrayList accounts = new ArrayList();
        foreach (ListItem li in LbAccounts.Items)
        {
           if (li.Selected == true)
            {
                accounts.Add(li);
            }
        }
}

 The li.selected always returns the first item in the listbox?!? If someone can tellme of a way to get around this and get the correct selected accounts, It will be very much appreciated!! Thanx in advance!!

 
Old June 4th, 2007, 05:55 AM
Registered User
 
Join Date: Jun 2007
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I'm have the same problem. Did you manage to solve it?
 
Old June 4th, 2007, 06:40 AM
Friend of Wrox
 
Join Date: Aug 2006
Posts: 109
Thanks: 0
Thanked 0 Times in 0 Posts
Default

okey I once had this problem and the reason was that I had two iteams with the same value.. check that :)

Nothing is impossible. The impossible only takes longer. "Digital Fortress, Dan Brown"
 
Old June 4th, 2007, 08:19 AM
Registered User
 
Join Date: Jun 2007
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

:) Ah, that did it.

Hmm... I was expecting the ListBox control to work by the index in the list rather than the item value.

Thanks loads


 
Old June 4th, 2007, 08:23 AM
Registered User
 
Join Date: Jun 2007
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Actually no, come to think of it the page will only send back the value from the ListItem so of course it wouldn't know which one was selected. Doh! Oops






Similar Threads
Thread Thread Starter Forum Replies Last Post
Number of selected items in a listbox sarah lee ASP.NET 2.0 Basics 2 May 17th, 2007 12:20 PM
Deleting selected row in ListBox YoungFools Excel VBA 1 April 19th, 2007 09:14 AM
Listbox problem (.Selected = True) DaDeViL VB Components 0 December 22nd, 2005 03:35 PM
selected items in asp:ListBox jtyson Classic ASP Basics 2 July 2nd, 2004 12:01 AM
How to get selected Item from Listbox? Iceman571 ADO.NET 1 March 25th, 2004 06:33 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.