ListItem item = new ListItem(text, value, enabled);
當然,也可以用
item.Enabled = false;
雖然在頁面中隱藏了,但依然可以用代碼來訪問隱藏的選項的,下面是一個例子
<%@ Page Language="C#" %>
<script runat="server">
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
ListItem[] items = new ListItem[]
{
new ListItem("Item 1", "Value 1"),
new ListItem("Item 2", "Value 2", false),
new ListItem("Item 3", "Value 3"),
new ListItem("Item 4", "Value 4")
};
ListBox1.Items.AddRange(items);
}
}
protected void Button1_Click(object sender, EventArgs e)
{
foreach (ListItem item in ListBox1.Items)
{
Response.Write(item.Text + " enabled=" + item.Enabled + "<br>");
}
}
</script>
<html XMLns="http://www.w3.org/1999/xHtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<ASP:ListBox ID="ListBox1" runat="server" />
<ASP:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" /></div>
</form>
</body>
</Html>