Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 2.0 > ASP.NET 2.0 Basics
Password Reminder
Register
| FAQ | Members List | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old April 10th, 2007, 03:12 PM
Registered User
 
Join Date: Apr 2007
Location: Atlant, , .
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!!

Reply With Quote
  #2 (permalink)  
Old June 4th, 2007, 05:55 AM
Registered User
 
Join Date: Jun 2007
Location: , , .
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I'm have the same problem. Did you manage to solve it?
Reply With Quote
  #3 (permalink)  
Old June 4th, 2007, 06:40 AM
Friend of Wrox
 
Join Date: Aug 2006
Location: Amman, , Jordan.
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"
Reply With Quote
  #4 (permalink)  
Old June 4th, 2007, 08:19 AM
Registered User
 
Join Date: Jun 2007
Location: , , .
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


Reply With Quote
  #5 (permalink)  
Old June 4th, 2007, 08:23 AM
Registered User
 
Join Date: Jun 2007
Location: , , .
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

Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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 Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


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



All times are GMT -4. The time now is 07:46 PM.


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