asp.net listbox set selected items
ListBox is an asp.net web server control. we can select a single item or multiple items at a item
from listbox control. in this example, we will see that how can we select multiple random items from listbox control
programmatically at run time.
To select listbox multiple items programmatically we need to apply few techniques. first we apply a linq query to find specific items from listbox items collection depends on developer's criteria. the linq query return a items collection from listbox which items we want to select programmatically. next we loop through the newly created items collection and set each item Selected property value to true. finally we get specified items are programmatically selected in listbox control.
The following asp.net c# example code demonstrate us how can we select listbox multiple items programmatically in an asp.net application.
To select listbox multiple items programmatically we need to apply few techniques. first we apply a linq query to find specific items from listbox items collection depends on developer's criteria. the linq query return a items collection from listbox which items we want to select programmatically. next we loop through the newly created items collection and set each item Selected property value to true. finally we get specified items are programmatically selected in listbox control.
The following asp.net c# example code demonstrate us how can we select listbox multiple items programmatically in an asp.net application.
set-selected-items.aspx
<%@ Page Language="C#" AutoEventWireup="true"%>
<!DOCTYPE html>
<script runat="server">
protected void Page_Load(object sender, EventArgs e)
{
if(!Page.IsPostBack)
{
string[] birds = {
"Rufous Scrubbird",
"Golden Bowerbird",
"Great Bowerbird",
"Andean Goose",
"Stitchbird",
"Spotted Bowerbird"
};
ListBox1.DataSource = birds;
ListBox1.DataBind();
ListBox1.Rows = ListBox1.Items.Count;
//find specific items from listbox
var items = from ListItem li in ListBox1.Items
where li.Text.Contains("Bowerbird")
select li;
//programmatically select specific items
//by default first time those items are selected.
foreach(ListItem li in items)
{
li.Selected = true;
}
}
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>asp.net listbox set selected items</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<h2 style="color:MidnightBlue; font-style:italic;">
asp.net example - set selected items
</h2>
<hr width="550" align="left" color="Gainsboro" />
<asp:Label
ID="Label1"
runat="server"
Text="select item(s) from listbox."
Font-Size="X-Large"
Width="350"
>
</asp:Label>
<br /><br />
<asp:ListBox
ID="ListBox1"
runat="server"
AutoPostBack="true"
Font-Size="X-Large"
SelectionMode="Multiple"
Font-Names="Comic Sans MS"
>
</asp:ListBox>
</div>
</form>
</body>
</html>

- How to sort ListBox items
- How to sort ListBox items alphabetically
- How to sort ListBox items by value
- How to set ListBox alternate item text and background color
- How to apply alternating row color in a ListBox
- How to show horizontal ScrollBar in a ListBox
- How to get ListBox multiple selected items
- How to disable specific items in a ListBox
- How to check whether an item exists in a ListBox
- How to add attributes to a ListBox items