ASP.NET 2.0中隐藏listbox的某一项

类别:.NET开发 点击:0 评论:0 推荐:
在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>


本文地址:http://com.8s8s.com/it/it42652.htm