<%@ Page Language="C#" AutoEventWireup="true"%>
<!DOCTYPE html>
<script runat="server">
protected void Page_Load(object sender, EventArgs e)
{
if(!Page.IsPostBack)
{
string[] birds = {
"Delhi",
"Mumbai",
"Agra",
"Bareilly",
"Patna",
"Gya"
};
ListBox1.DataSource = birds;
ListBox1.DataBind();
ListBox1ListBox1.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>
<!DOCTYPE html>
<script runat="server">
protected void Page_Load(object sender, EventArgs e)
{
if(!Page.IsPostBack)
{
string[] birds = {
"Delhi",
"Mumbai",
"Agra",
"Bareilly",
"Patna",
"Gya"
};
ListBox1.DataSource = birds;
ListBox1.DataBind();
ListBox1ListBox1.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>
No comments:
Post a Comment