ListBox控件

呈现形态

<select size=”4” name=”lb_cai” multiple=”multiple”id=”lb_cai”>
     <option value=”1”>糖醋排骨</option>
     <option value=”2”>红烧鸡块</option>
     <option value=”3”>清蒸鲍鱼</option>
     <option value=”4”>酸菜鱼</option>
     <option value=”5”>香辣鸡翅</option>
</select>

控件语句

<asp:ListBox ID=”lb_food” runat=”server” selectionMode=”multiple”>
  <asp:ListItem value=”1”>糖醋排骨</asp:ListItem>
  <asp:ListItem value=”2”>红烧鸡块</asp:ListItem>
  <asp:ListItem value=”3”>清蒸鲍鱼</asp:ListItem>
  <asp:ListItem value=”4”>酸菜鱼</asp:ListItem>
</asp:ListBox>

ListBox可多选

   可调用属性与DropDownList相同

                      Ibox_selectIndex                只返回一个数值(0,1,2,3)

                      Ibox_selectValue                返回字符串value的值

                      Ibox_selectedItem.text           返回字符串(显示的文本内容)

 

可多选,就表示我们编程时常需要对控件下的ListItem集合进行遍历操作

 

 

.NET

<body>
    <form id="form1" runat="server">
    <div>
<asp:ListBox ID="ListBox1" runat="server" SelectionMode="Multiple" Height="159px" Width="250px" CssClass="option">
            <asp:ListItem Value="1">糖醋排骨</asp:ListItem>
            <asp:ListItem Value="2">酸菜鱼</asp:ListItem>
            <asp:listitem Value="3">胡辣子兔</asp:listitem>
             <asp:ListItem Value="4">西瓜</asp:ListItem>
            <asp:listitem Value="5">包子</asp:listitem>
             <asp:ListItem Value="6">黄瓜炒蛋</asp:ListItem>
            <asp:listitem Value="7">扬州炒饭</asp:listitem>
             <asp:ListItem Value="8">青岛啤酒</asp:ListItem>
            <asp:listitem Value="9">狗不理包子</asp:listitem>
        </asp:ListBox>
        <asp:Button ID="Button1" runat="server" Text="确定" OnClick="Button1_Click" />
        <asp:TextBox ID="TextBox1" runat="server" TextMode="MultiLine" Height="150px" Width="247px"></asp:TextBox>
    </div>
    </form>
</body>
</html> 



C#


 protected void Button1_Click(object sender, EventArgs e)
    {
        //第一种方法
        TextBox1.Text="";  //每次选择后textbox1中的记录清空
        //for (int i = 0; i < ListBox1.Items.Count; i++)
        //{
        //    if (ListBox1.Items[i].Selected)
        //    {
        //        TextBox1.Text += ListBox1.Items[i].Text + ",";
        //    }
        //}

        //第二种方法
        foreach (ListItem item in ListBox1.Items)
        {

            if (item.Selected)
            {
                TextBox1.Text += item.Text+",";
            }
        }

    }

对Items的删除

Obj.Items.add(Obj_ListItem);         添加

 

Obj.Items.Remove(Obj_ListItem);         删除一个特定的ListItem

 

Obj.Items.Removeat(i);             产出索引号为I的这个项

 

.NET

<body>
    <form id="form1" runat="server">
    <div>
<asp:ListBox ID="ListBox1" runat="server" SelectionMode="Multiple" Height="159px" Width="250px" CssClass="option">
            <asp:ListItem Value="1">糖醋排骨</asp:ListItem>
            <asp:ListItem Value="2">酸菜鱼</asp:ListItem>
            <asp:listitem Value="3">胡辣子兔</asp:listitem>
             <asp:ListItem Value="4">西瓜</asp:ListItem>
            <asp:listitem Value="5">包子</asp:listitem>
             <asp:ListItem Value="6">黄瓜炒蛋</asp:ListItem>
            <asp:listitem Value="7">扬州炒饭</asp:listitem>
             <asp:ListItem Value="8">青岛啤酒</asp:ListItem>
            <asp:listitem Value="9">狗不理包子</asp:listitem>
        </asp:ListBox>
        <asp:Button ID="Button1" runat="server" Text="确定" OnClick="Button1_Click" />
        <asp:TextBox ID="TextBox1" runat="server" TextMode="MultiLine" Height="150px" Width="247px"></asp:TextBox>
    </div>
    </form>
</body>
</html> 



C#


 protected void Button1_Click(object sender, EventArgs e)
    {
        //第一种方法
        TextBox1.Text="";  //每次选择后textbox1中的记录清空
        //for (int i = 0; i < ListBox1.Items.Count; i++)
        //{
        //    if (ListBox1.Items[i].Selected)
        //    {
        //        TextBox1.Text += ListBox1.Items[i].Text + ",";
        //    }
        //}

        //第二种方法
        foreach (ListItem item in ListBox1.Items)
        {

            if (item.Selected)
            {
                TextBox1.Text += item.Text+",";
            }
        }

    }








    protected void Button2_Click(object sender, EventArgs e)
    {
        //反向遍历  用正向遍历的话 两个连续的只能提交一个上去(酸菜鱼、牛肉  只能上去其中一个)
        //不能foreach遍历  因为会破坏集合报错。
        for (int i = ListBox2.Items.Count-1; i >=0; i--)
        {
            if (ListBox2.Items[i].Selected)
            {
                ListBox3.Items.Add(ListBox2.Items[i]); //listbox3集合中添加listbox2集合删除的
                ListBox2.Items.RemoveAt(i);  //移除listbox2中已经选择的条目  
            }
        }
    }
    protected void Button3_Click(object sender, EventArgs e)
    {
        for (int i = ListBox3.Items.Count - 1; i >= 0; i--)
        {
            if (ListBox3.Items[i].Selected)
            {
                ListBox2.Items.Add(ListBox3.Items[i]); 
                ListBox3.Items.RemoveAt(i);  
            }
        }
    }

猜你喜欢

转载自www.cnblogs.com/xiaowie/p/9150132.html