.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Top 5 Contributors of the Month
Gaurav Pal
Post New Web Links

ListBox Iteration

Posted By:      Posted Date: September 11, 2010    Points: 0   Category :ASP.Net
Hello,     I have a listbox that i need to iterate and add a comma and apostrophe so that i can pass the values to a query   this is what i got so far   StringBuilder sb = new StringBuilder(); foreach (ListItem item in ListBox1.Items) { sb.Append(item); // Never use string concat here sb.Append(','); } if (sb.Length > 0) { sb.Length--; } string ret = sb.ToString(); Label1.Text = ret.ToString(); } This adds the "," but how can i add the "  '  "?    Thank you in advance 

View Complete Post

More Related Resource Links

ListBox tricky to set in a DataGrid/GridView

Listboxes are very tricky to set selected items in grid - there is no real nice way to do it. It would have been great if Microsoft included a property that accepts an array or comma separated values - or anything! But they only allow each item in the list to be selected individually. The way to do this in a grid is a bit messy, but it is fairly simple to implement. When a row is created (as per the event), select the items of the listbox as per the datagrid source data:

Working with DropDownList and ListBox Controls in ASP.NET

"In my application i am using dropdownlist to display all categories from a table. When the user clicks this control I display all records in that category. Category table also contains column called color.

I wanted display some categories in dropdown list different color and others in different color using ASP.NET."-Anonymous

Persist data in a dynamically populated ListBox in a user control


Hi All,

I am trying to persist data in a dynamically populated ListBox in a user control. Here are the details

I have a user control which contains a a listBox and button. On Page_Load of user control I am populating the ListBox control with some values. On Click of button, I am trying to fetch the values which I have selected in ListBox. Though this seems very simple and straight forward I am unable to fetch values.

My Page_Load Function

void Page_Load(object sender, EventArgs e)










My Click Function

Protected Void Button1_Click(object sender, EventArgs e)


       string strValue = listBox.SelectedValue



I have placed brake point at Page_Load and on click function, I am seeing that the moment the breakpoin

ActiveX and Visual Basic: Enhance the Display of Long Text Strings in a Combobox or Listbox


The combobox and listbox controls in Visual Basic have no built-in support for displaying text strings that are too long to fit in the visible area of either control. This article describes several techniques to improve the readability of long strings in each control. The first technique uses tooltips to display a combobox or listbox item. The other techniques, which use various Windows APIs, include adding a horizontal scrollbar for a listbox and determining the necessary width of the longest string given the font attributes of the text. The required steps are described for both comboboxes and listboxes.

John Calvert

MSDN Magazine December 2000

Datalist with embedded Grid or List...Sub Totals issue with iteration


Let me see if one of you can assist with this one...


i have a similar situation, but mine does not use the selected item (item Command). Here is what the form is to do:


1. this is for an educational environment, so we use SID- NOT SSN's, but i use the SID as a filter in the subset as the grouping unique id.

2. I'm trying to inject a sub total row at the last line item of each SID (if more than one item in the report for that SID).

3. I'm looping thru the items in the Datalist Item Command with a for each (item i in Datalist2.Items).

setting scroll position for a ListBox


I have a ListBox control showing 10 rows.  I have 30 rows total in the listbox.  How do I programmatically adjust the scroll to show the last ten rows?

Some sources have suggested that I set the SelectedItem.  But I am already making selections (myListBox.Items[28].Selected = true, myListBox.Items[29].Selected = true, etc); the listbox scroll is positioned on Item[0] not the first selected item.  I have also tried setting myListBox.SelectedItem in addition to the multiple selection just mentioned but that does not have any affect either.


Filter a listbox with textbox


So i currently filter the listbox with some javascript i have found.
What it does is going to the item i type on the textbox.
It will not filter the listbox so it will contain the specific matches.
I was reading for autocomplete but i don't seem to find this 2 way usage.
I was also reading this article:
But i'm completely lost on how i can implement it.
Any ideas?

How to reference a listbox from a procedure


I have a listbox that gets loaded from a couple of different places on my web form.

I would like to put the code to load the listbox in one place, rather than have two copies of it.

The problem is, I can't figure out how to reference the listbox control successfully from within a procedure.

Would anyone know how to do this?

My code looks like this:

private static bool LoadSubscriptionListBox(System.Web.UI.Page Page)
        bool bReturn = false;

        // Clear the listbox

        // Load the listbox



jQuery modified ListBox not posting back modified values


Hi everyone,

I have an ASP.Net ListBox that I'm trying to populate via jQuery, using the following snippet:

        $("#MyList_btnAddAll").click(function(e) {
        $('#MyList_lstAll option').appendTo('#MyList_lstSelected');

The code has two ListBoxes in fact, one a "source" and the other a "destination".  As you can tell above the ListBoxes are MyList_lstAll and MyList_lstSelected.  These are rendered in the browser as <select> elements, as you'd expect.

The jQuery is working great, the items are moving from one ListBox to the other, the DOM is updated but when I submit this form (not using jQuery), the ListBoxes don't reflect this change on postback. 

I realize my modifications via jQuery aren't available in ViewState but I thought since the DOM was updated these changes would be part of the postback data?  But in the codebehind after postback the ListBox contents haven't changed.  Does anyone know what might be going on and what I can do about it?  Many thanks.

How to create a dynamically populated items in a listbox and use it as a navigation?

Hello, I am trying to create navigation inside a listbox menu. I load data from xml to populate a list. Each item in the list box should be a link or button. Then, I create a frame to load xaml pages in it. I tested to load pages employing button control, outside listbox with on click event and it worked well. However, I cannot make it work with list box items. I am hoping to get help from you. Any sample or ideas are highly appreciated. Below is the code:  XML: <?xml version="1.0" encoding="utf-8"?> <Pages> <page id="page01"> <name>Page 01</name> </page> <page id="page02"> <name>Page 02</name> </page> </Pages> C# to initiate a click o n listbox item and get the path to page: private void SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e) { ListBoxItem lbi = ((sender as ListBox).SelectedItem as ListBoxItem); string itemName = lbi.Content.ToString(); if ( Nav_ListBox.SelectedItem.Equals("Page01" ) ) { ContentFrame.Source = new Uri("Pages/Page01.xaml", UriKind.RelativeOrAbsolute); } } XAML:  <ListBox x:Name="Nav_ListBox" Margin="0" ScrollViewer.HorizontalScrollBarVisibility="Hidden" Background="#FFF2F2F2" ItemTemplate="{DynamicReso

Page to page navigation from listbox menu?

I am wondering if anyone can share with good sample on how to create menu populated in a listbox from xml and have page to page navigation between xaml pages. I need to have a unique Uri link associated with each item in the listbox. Any advice is highly appreciated. Thank you in advance,

Data binding a listbox

I'm using VS 2010 and writing a WPF application.  I have a listbox on a form that I want bound to an access database field.  In the code behind I create the usual oledb connection, command and adapter and run a query.  In debug I can see the dataset has 143 items from the database, a single column called "path".  Here's the relevant code:  oleCon.Open(); adpt.SelectCommand = oleComd; adpt.Fill(dtst,"MyTable"); MusicList.DataContext = dtst; MusicList is the listbox.  There's 143 items in the dataset.  When the form loads the listbox is empty.  Here's the xaml for the listbox: <ListBox Grid.Row="1" Height="477" HorizontalAlignment="Left" Margin="7,6,0,0" Name="MusicList" VerticalAlignment="Top" Width="310" Grid.RowSpan="2" Background="#002400" ItemsSource="{Binding Path=path, Mode=OneTime}" DataContext="{Binding}"> <ListBox.ItemTemplate> <DataTemplate> <StackPanel> <TextBlock Text="{Binding Path=path}" /> </StackPanel> </DataTemplate> </ListBox.ItemTemplate> </ListBox> At least I know the data is present but why isn't it binding to the listbox? Thanks!  

using CompositeCollection in ListBox for merging two Typed DataTable

Hi i have a Typed DataSet contining two DataTable : Table1 , Table2 i want to merge the two Tables in a ListBox by using "CompositeCollection " , but this doesn't work <ListBox.ItemsSource> <CompositeCollection> <CollectionContainer Collection="{Binding Path = Table1}"/> <CollectionContainer Collection="{Binding Path =Table2}"/> </CompositeCollection> </ListBox.ItemsSource> i put also : public partial class Window1 : Window { public Window1() { InitializeComponent(); this.FillDataset(); this.DataContext = ds; //this.Resources.Add("mytable1", DataSet1.Table1); }   i want to confess i don't have a clear idea about referencing an object as a Typed DataTable. also if a want to Template my ListBox lique this :    <ListBox.ItemTemplate> <DataTemplate DataType="{x:Type myprog:DataSet1+Table1 }"> <StackPanel Orientation="Horizontal"> <TextBlock Text="{Binding Path=ProductName}" Background="AliceBlue"/> </StackPanel> </DataTemplate> <DataTemplate DataType="{x:Type myprog:DataSet1+Table2 }"> <StackPanel Orientation="Horizontal"> <TextBlock Text="{Binding Path=ProductName}&quo

Sorting a ListBox or ComboBox on a Converted Value

Is it possible to apply a Converter to the SortDescription of a CollectionViewSource?  My ViewModel contains a List<> of a class that contains an enum and a Boolean.  My XAML displays a multi-select ListBox, showing the localized names of the enum values using a Converter in the View, and binds each ListBoxItem's IsSelected to the Boolean in the members of the bound List<>.  That's working.  Now I want to sort the ListBox items by those localized enum names, without reordering the List<> in the ViewModel.  Is this possible, and if so, how can it be done?

How can I change the SelectedIndex on a listbox when another listbox is doubleclicked?

Here is my xaml. <     StackPanel>       <ListBox x:Name="List1">       <ListBoxItem>Part 1</ListBoxItem>       <ListBoxItem>Part 2</ListBoxItem>       <ListBoxItem>Part 3</ListBoxItem>       <ListBoxItem>Part 4</ListBoxItem>       <ListBox.Triggers>       <EventTrigger RoutedEvent="ListBox.MouseDoubleClick">       <BeginStoryboard>       <Storyboard Duration="00:00:00">       <Int32Animation Storyboard.TargetName="List2" Storyboard.TargetProperty="SelectedIndex" From="-1" To="0" Duration="00:00:00" FillBehavior="HoldEnd"></Int32Animation>       </Storyboard>       </BeginStoryboard>       </EventTrigger>       </ListBox.Triggers>       </ListBox>       <ListBox x:Name="List2">       <ListBoxItem>Section A</ListBoxItem>       <ListBoxItem>Section B</ListBoxItem>

Get bounding rectangle of a listbox item

Hi everyone, I can add a String or a ListBoxItem into Listbox. When item of Listbox is a ListBoxItem , I can get ListBoxItem 's rectangle by calling "ListBoxItem.PointToScreen(point)". But I can not get rectangle of an item if this item is a String . Any help would be appreciated!

Show folder content in a ListBox

Hi. I am close to going crazy here :) I have a small app and I want people to be able to open some files from within it. All I want is a small control that shows me the content of a specific folder on the HDD. My current approach includes a ListBox which should be filled with the documents of that folder. However I have (even after hours of googling and binging^^) absolutely no idea how to get them in there. I hoped that you could databind your ListBox to the file system, but this does not seem to be possible. Do I have to fill it via c# code? If so, how?   Any help would be greatly appreciated.  
ASP.NetWindows Application  .NET Framework  C#  VB.Net  ADO.Net  
Sql Server  SharePoint  Silverlight  Others  All   

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend