listbox.SelectedIndex

setting multiple values in RUNTIME

Page 1 of 1

3 Replies - 16895 Views - Last Post: 11 June 2010 - 08:43 AM Rate Topic: -----

#1 Guest_kitzur*


Reputation:

listbox.SelectedIndex

Posted 10 June 2010 - 01:35 AM

I know that the web's full of Q's and A's about how to use IsPostBack when selecting the value of a listbox.
My question's a bit different.
I have a stored procedure that returns a few values, and I want in runtime to have all the values be selected in the listbox.
But when I loop through the SqlDataReader and do: listbox.SelectedValue = reader(0), the final result on the browser is that only the last value gets selected.
The listbox's SelectionMode is "Multiple".

Thank you very much

Is This A Good Question/Topic? 0

Replies To: listbox.SelectedIndex

#2 olibenu  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 45
  • View blog
  • Posts: 537
  • Joined: 31-December 08

Re: listbox.SelectedIndex

Posted 10 June 2010 - 03:16 AM

i've only used a checked listbox for this.
the listbox should have a selecteditems collection, try adding items manually.
Was This Post Helpful? 0
  • +
  • -

#3 CharlieMay  Icon User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1605
  • View blog
  • Posts: 5,162
  • Joined: 25-September 09

Re: listbox.SelectedIndex

Posted 10 June 2010 - 11:06 AM

Here's one way to select all items in a multi-select listbox.
        For x As Integer = 0 To ListBox1.Items.Count - 1
            ListBox1.SelectedIndex = x
        Next

Was This Post Helpful? 0
  • +
  • -

#4 Frinavale  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 203
  • View blog
  • Posts: 776
  • Joined: 03-June 10

Re: listbox.SelectedIndex

Posted 11 June 2010 - 08:43 AM

First thing you need to do is make sure that the ListBox is configured to allow multiple items to be selected. You do this by setting the ListBox.SelectionMode property.

Selecting multiple items is going to depend on how you add the items to the ListBox.

There are a few ways that you can add items to a ListBox.

The first way would be to do so declarative in your ASP.NET code. I know you're not taking this approach but if someone else were to they could set the selected values in the declarative code like this:
<asp:ListBox ID="ListBox1" runat="server" SelectionMode="Multiple">
    <asp:ListItem Text="Red" Value="#FF0000" Selected="True" />
    <asp:ListItem Text="Blue" Value="#0000FF" Selected="True"/>
    <asp:ListItem Text="Green" Value="#008000" Selected="True" />
</asp:ListBox>




The second way to add items to a ListBox is to loop each record you retrieve, create a new ListItem for the record and add it to the ListBox. If you take this approach, then you can either set that item to be selected when you add it, or later (like in the Page PreRender event) you could loop through the items in the ListBox and set them to be selected).
For example:
(VB.NET code)
'Don't have a data source so I'm using a Dictionary instead. '
Dim records As New Dictionary(Of String, String)
records.Add("Red", "#FF0000")
records.Add("Blue", "#0000FF")
records.Add("Green", "#008000")

'Populating the ListBox with ListItems '
For Each key in records.Keys
    Dim value As String = records(key)
    Dim item ListItem = new ListItem(key, value)
    'At this point you could set the ListItem to be selected. '
    'But for example purposes I am not going to do that '
    'so that I can demonstrate how to loop through the items '
    'item.Selected = true '
    ListBox1.Items.Add(item)
Next

'Looping through each ListItem and selecting it '
For Each item in ListBox1.Items
    item.Selected = true
Next



(C# code)
//Don't have a data source so I'm using a Dictionary instead.
Dictionary<String, String> records = new Dictionary<string, string>();
records.Add("Red", "#FF0000");
records.Add("Blue", "#0000FF");
records.Add("Green", "#008000");

//Populating the ListBox with ListItems
foreach(string key in records.Keys){
    string value = records[key];
    ListItem item = new ListItem(key, value);
    //At this point you could set the ListItem to be selected.
    //But for example purposes I'm not going to do that
    //so that I can demonstrate how to loop through the items
    //item.Selected = true;
    ListBox1.Items.Add(item);
}

//Looping through each ListItem and selecting it
foreach (ListItem item in ListBox1.Items) {
    item.Selected = true;
}





The last way that you can add items to a ListBox is to bind it to a datasource (like a DataTable). In this case you're going to have to loop through the ListBox.Items after you call the ListBox.DataBind() method.
(VB.NET code)
Dim records As New Dictionary(Of String, String)
records.Add("Red", "#FF0000")
records.Add("Blue", "#0000FF")
records.Add("Green", "#008000")

'Creating a DataTable to use a DataSource for the ListBox '
Dim dt As New DataTable
dt.Columns.Add(new DataColumn("Colour", GetType(String)))
dt.Columns.Add(new DataColumn("Value", GetType(String)))

'Populating the DataTable (datasource) with data '
For Each key in records.Keys
    Dim value As String = records(key)
    Dim row As DataRow = dt.NewRow()
    row("Colour") = key
    row("Value") = value
    dt.Rows.Add(row)
Next

ListBox1.DataSource = dt
ListBox1.DataTextField = "Colour"
ListBox1.DataValueField = "Value"
ListBox1.DataBind()


'Looping through each ListItem and selecting it '
For Each item in ListBox1.Items
    item.Selected = true
Next



(C#)
Dictionary<String, String> records = new Dictionary<string, string>();
records.Add("Red", "#FF0000");
records.Add("Blue", "#0000FF");
records.Add("Green", "#008000");

//Creating a DataTable to use a DataSource for the ListBox 
DataTable dt = new DataTable();
dt.Columns.Add(new DataColumn("Colour", typeof(String)));
dt.Columns.Add(new DataColumn("Value", typeof(String)));


//Populating the DataTable (datasource) with data 
foreach (string key in records.Keys)
{
    string value = records[key];
    DataRow row = dt.NewRow();
    row["Colour"] = key;
    row["Value"] = value;
    dt.Rows.Add(row);
}

ListBox1.DataSource = dt;
ListBox1.DataTextField = "Colour";
ListBox1.DataValueField = "Value";
ListBox1.DataBind();          
//Looping through each ListItem and selecting it
foreach (ListItem item in ListBox1.Items) {
    item.Selected = true;
}


Happy Coding,

-Frinny

This post has been edited by Frinavale: 11 June 2010 - 08:45 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1