Obviously a list box should display its list of items. By default, it is made to display 4. This characteristics is controlled by an attribute named Rows. To specify the number of items that are visible at one time, you can assign the desired value to this attribute. Here is an example: <%@ Page Language="C#" %> <html> <head> <title>Exercise</title> </head> <body> <form id="frmRealEstate" method="post" runat="server"> <asp:ListBox id="TypesOfHouses" rows="4" runat="server"> <asp:ListItem>Condominium</asp:ListItem> <asp:ListItem>Townhouse</asp:ListItem> <asp:ListItem>Single Family</asp:ListItem> <asp:ListItem>Trailer</asp:ListItem> </asp:ListBox> </form> </body> </html> This would produce: If the list contains less than 4 items, there would be an empty area under the last item: If there are are more than 4 items, the list box would display a scroll bar on its right side. Here is an example: <%@ Page Language="C#" %> <html> <head> <title>Exercise</title> </head> <body> <form id="frmRealEstate" method="post" runat="server"> <asp:ListBox id="TypesOfHouses" rows="4" runat="server"> <asp:ListItem>Condo Studio</asp:ListItem> <asp:ListItem>Condo Apartment</asp:ListItem> <asp:ListItem>Townhouse</asp:ListItem> <asp:ListItem>Single Family</asp:ListItem> <asp:ListItem>Trailer</asp:ListItem> <asp:ListItem>Unknown</asp:ListItem> </asp:ListBox> </form> </body> </html>
If a list box contains more than one item, you can control whether the user is allowed to select only one or more than one item at a time. To assist you with this, the <asp:ListBox> tag is equipped with an attribute named SelectionMode. Its two values are Single and Multiple and they are members of the ListSelectionMode enumeration. By default, a list box allows the user to select only one item because the default value of the SelectionMode attribute is Single. If you want to allow the user to bee able to select many items, assign the Multiple string to this attribute.
As we know by now, to select an item from a list box, the user can click that item. The selected item is highlighted. A user can also be allowed to select more than one item. This operation would highlight the items that were selected. To catch this action, you have two options. To use the index of the item that was clicked, when a selection is made, the control fires the SelectedIndexChanged event. When a user selects an item on the list, the control identifies the text of that item and updates itself. Based on this, the control fires an event named TextChanged. You can use one of these events to take action when a selection has been made and sent to the server. |
|
|||||||
|