日期:2014-01-02  浏览次数:20424 次

在asp.net 2.0中,可以隐藏listbox中的某一项,比如
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>