ListBox
ADDING ITEMS
lst_MyBox.Items.Add(txt_MyTextBox.Text);
SELECTED ITEM
if (ListBox1.SelectedItem != null)
Label1.Text = "The selected item is: " + ListBox1.SelectedItem.Text;
USING SIMPLE DATA BINDING WITH LISTS
1. Add a listbox or DropDownListBox to your WebForm
2. Add a string array to your class
E.g. public string[] arrData = {"This", "that", "and", "the", "other"};
3. On the Page_Load or other event (e.g. Click) add the following
E.g. Page.DataBind();
4. Select the control on the WebForm page
5. Click the button in the Databindings property
6. Select "DataSource" in the Bindable Properties list
7. Select the Custom Binding Expression option button
8. Type the name of your string array (e.g. arrData) in the expressions box
9. Click 'OK'
When you use binding with a server control, you can turn off state management for that control. This improves performance
because the "DataBind" method replaces the automatic view state management provided by ASP.NET
BINDING FROM A COLLECTION
private void Page_Load( )
strPath = Session["Path"].ToString(); // Put Session variable in class variable
if (!Page.IsPostBack) // If this is not a post-back event.
{ strFiles = Directory.GetFiles(strPath); // Get list of files in the current directory
for (int iCount = 0; iCount <= strFiles.GetUpperBound(0); iCount++) // Get the short names for the files
strFiles[iCount] = Path.GetFileName(strFiles[iCount]); } // and populate the array
lstFiles.DataBind(); // Bind lstFiles to file array
// NOTE: Make sure the DataSource for the listbox is set to "strFiles", an array
To turn off state management for a server control, set the control's "EnableViewState" property to 'False'
REMOVE FROM LISTBOX
lstFiles.Items.Remove(lstFiles.SelectedItem);