Saturday 23 June 2012

ListBox

List box is used to display a list of one or more item so that user can choose one or more items from it.List box displays list of item sequentially and vertically in a scrollable window.

You can add values in Listbox manually or programmatically.To add values manually firstly add your listbox control into webpage and select it a small tag will appear on right side of listbox click on it and select Edit Items.
After click on Edit Items a ListItems Collection Editor box will appear .Click on Add button and enter your item's text in Text field and item's value in Value field.Your item will successfully added into listbox.Similarly you can add more values into listbox.At last click on OK button.
Similarly you can add values into listbox from database.To add values from database click on small tag of listbox and select Choose Data Source.A new Data Source Configuration Wizard dialog box will appear.Now create a new data source or select a already created data source.
After selection of Data Source click on OK button.
To add values in listbox programmatically use the following code in PageLoad event.

   
 protected void Page_Load(object sender, EventArgs e)
    {
        if(!IsPostBack)
        {
            ListBox1.Items.Add(new ListItem("Pkistan", "Pkistan"));
            ListBox1.Items.Add(new ListItem("Canada", "Canada"));
            ListBox1.Items.Add(new ListItem("Paris", "Paris"));
            ListBox1.Items.Add(new ListItem("UK", "UK"));
            ListBox1.Items.Add(new ListItem("Itly", "Itly"));
        }
    }
This code add the values in the listbox at run time.


Some Commonly Used Properties of ListBox
SelectedIndex Property:
Gets or Sets the lowest index of the selected item in the listbox.This property will return the index as an integer.
SelectedItem Property:
Gets or Sets the selected item with the lowest index in the listbox control.
SelectedValue Property:
Gets the value of the selected item in the listbox control.This property will return the value as a string.
SelectionMode Property:
Sets the selection mode of the listbox control.By using this property you can select one or more values from the listbox.











No comments:

Post a Comment