Tuesday, September 22, 2015

ASP.NET ListBox control

Suggested Videos
Part 22 - Cascading dropdown asp.net
Part 23 - Asp.net checkboxlist control
Part 24 - Asp.net checkboxlist, select or deselect all list items

In this video we will discuss about ListBox control. Just like DropDownList and CheckBoxList, ListBox control is also a collection of ListItem objects. Working with ListBox control is very similar to DropDownList and CheckBoxList. Adding items and binding to a datasource is exactly similar. In this part of the video, let's discuss about the properties that are specific to the ListBox control.

Rows : The number of visible rows in the Listbox. A scrollbar is automatically generated, if the total number of item are greater than the number of visible rows in the listbox.
SelectionMode : SelectionMode can be Single or Multimple. By default, this property value is Single, meaning when the listbox renders, the user can select only one item from the listbox. Set this property to Multimple, to enable multiple item selections. To select, multiple items from the listbox, hold-down the CTRL key, while the listitem's are selected.

Please note that, it is not possible to set the Selected property of more than one ListItem object to true, if the SelectionMode of the listbox is to Single.

Retrieving the selected item's Text, Value and Index is similar to DropDownList and CheckBoxList

ASPX Page code:
<asp:ListBox ID="ListBox1" runat="server" SelectionMode="Multiple">
    <asp:ListItem Text="Diploma" Value="1"></asp:ListItem>
    <asp:ListItem Text="Graduate" Value="2"></asp:ListItem>
    <asp:ListItem Text="Post Graduate" Value="3"></asp:ListItem>
    <asp:ListItem Text="Doctrate" Value="4"></asp:ListItem>
<br />
<br />
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />

Code Behind Code:
protected void Button1_Click(object sender, EventArgs e)
    foreach (ListItem li in ListBox1.Items)
        if (li.Selected)
            Response.Write("Text = " + li.Text + ", ");
            Response.Write("Value = " + li.Value + ", ");
            Response.Write("Index = " + ListBox1.Items.IndexOf(li).ToString());

ASP.NET ListBox Example:

If you are searching life partner. your searching end with kpmarriage.com. now kpmarriage.com offer free matrimonial website which offer free message, free chat, free view contact information. so register here : kpmarriage.com- Free matrimonial website