- ListBox Web Server Control
in ASP.NET environment,It is varing from DropdownList Control.The ListBox Web server control enables users to select one or more items from a predefined list.t can display multiple items at once and it optionally enables the user to select multiple items.
Customize Listbox Attributes
- Rows -this enables you to provides the number of rows(items) of ListBox control.suppose it has more items rather than specified rows, then it automatically enables the scroll bar.
- Width Set the Width of the ListBox control in pixel.but it conceals the text when it exeed the width what you have specified
- Height Set the Height of the ListBox control in pixel.but it conceals the Rows(items) when it exeed the height what you have specified
- SelectionMode Set the SelectionMode Property such as single, multiple to allow the user to select single or multiple items respectively
- Single Or Multiple Selection
Users can normally select a single item in the list by clicking it. If you set the ListBox control to enable multiple selections, users can hold down the CTRL or SHIFT key while clicking to select multiple items.
- Add Items in List
When you add a list item, you must specify the three properties of the item as,
- Text - The text displayed in the list.
- Value -The value associated with an item. Setting this property allows you to associate a value with a specific item without displaying it.
- Selected -A Boolean value indicating whether the item is selected.
- To add items at design time
When you add a list item, you must specify the three properties of the item as,
- Type an <asp:ListItem> element in the list control.
- Set the Text and Value properties of the new list item.
- Optionally, set the Selected property for one items.
the following code snippets demonstrates a listbox control
-
<asp:ListBox ID="ListBox1" runat="server">
<asp:ListItem Text="Apple" Value="App" Selected="True" />
<asp:ListItem Text="Orange" Value="Ora" />
<asp:ListItem Text="Banana" Value="Ban" />
</asp:ListBox>
-
- To add items programmatically
- Create a new object of type ListItem and set its Text and Value properties.
- Call the Add method of the control's Items collection and pass it the new object.
the following code snippets demonstrates this
-
Protected void Button1_Click (object sender, System.EventArgs e)
{
ListBox1.Items.Add(new ListItem("Apple", "App"));
ListBox1.Items.Add(new ListItem("Orange", "Ora"));
ListBox1.Items.Add(new ListItem("Banana", "Ban"));
}
-
- To add items programmatically via Data Source
- set the control's AppendDataBoundItems property to true.
- Add a data source control to the page, such as a SqlDataSource or ObjectDataSource control. For details, see Data Source Controls Overview.
- Configure the data source control with connection string and query information.
- Set the list control's DataSourceID property to the ID of the data source control.
- Set the Properties such as DataTextField and DataTextFormatString
the following code snippets demonstrates this
-
<asp:ListBox ID="ListBox1" runat="server"
DataSourceID="SqlDataSource1"
DataTextField="CategoryName"
DataValueField="CategoryID">
</asp:ListBox>
<asp:SqlDataSource
ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>"
ID="SqlDataSource1"
runat="server"
SelectCommand="SELECT [CategoryID], [CategoryName] FROM [Categories]">
</asp:SqlDataSource>
-
- Handling SelectedIndexChanged() event
within the SelectedIndexChanged event, By default, the SelectedIndexChanged event does not immediately cause the page to be posted to the server. Instead, the event is raised in server code the next time the form is posted. To perform the SelectedIndexChanged event, set the control's AutoPostBack property to true.
the following code snippets demonstrates this
-
Protected void DropDownList1_SelectedIndexChanged(object sender,
System.EventArgs e)
{
Label1.Text = "You selected " + DropDownList1.SelectedItem.Text;
}
-
- Related Links
-
6cd653cf-dc7d-4ace-8cf6-c2030f0403ff|0|.0
3cb86a75-c154-4d01-b91a-e1c650f5c63e|0|.0