添加ListItem控件,具体实现如下:
添加DropDownList控件:
Note:将DropDownList1控件赋值给obj_dd1控件,之后对obj_dd1控件进行操作,实质仍是对原控件进行操作;
没有产生新的控件,只是换个名称来操纵原控件;
删除被选中的ListItem控件:
注意ListBox的选择模式:
反向遍历的具体代码实现:
点菜系统完整代码:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default3.aspx.cs" Inherits="Default3" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ListBox ID="ListBox1" runat="server" SelectionMode="Multiple" Height="148px" OnSelectedIndexChanged="ListBox1_SelectedIndexChanged" Width="205px">
<asp:ListItem>四喜丸子</asp:ListItem>
<asp:ListItem>溜肉段</asp:ListItem>
<asp:ListItem>锅包肉</asp:ListItem>
<asp:ListItem>软炸里脊</asp:ListItem>
<asp:ListItem>日本豆腐</asp:ListItem>
<asp:ListItem>麻辣水煮鱼</asp:ListItem>
</asp:ListBox>
<asp:Button ID="Button1" runat="server" Text="点菜" OnClick="Button1_Click" Width="67px" />
</div>
<asp:ListBox ID="ListBox2" runat="server" SelectionMode="Multiple" Height="135px" Width="198px"></asp:ListBox>
<asp:Button ID="Button2" runat="server" Text="取消点菜" OnClick="Button2_Click" />
</form>
</body>
</html>
protected void Button1_Click(object sender, EventArgs e)
{
for (int i = ListBox1.Items.Count - 1; i >= 0; i--)
{
if (ListBox1.Items[i].Selected)
{
ListBox2.Items.Add(ListBox1.Items[i]);
ListBox1.Items.RemoveAt(i);
}
}
}
protected void Button2_Click(object sender, EventArgs e)
{
for (int i = ListBox2.Items.Count - 1; i >= 0; i--)
{
if (ListBox2.Items[i].Selected)
{
ListBox1.Items.Add(ListBox2.Items[i]);
ListBox2.Items.RemoveAt(i);
}
}
}