<% ... @ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %> <! DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" > < html xmlns ="http://www.w3.org/1999/xhtml" > < head runat ="server" > < title > 无标题页 </ title > </ head > < body > < form id ="form1" runat ="server" > < div style ="color: #3366ff" > 列表框 ListBox 我也不知道怎么形容这个,反正网上用得很多!他的大概功能就是把下拉列表给长高了,然后不能从下面伸一截出来了,但是他一样可以装很多东西,而且有一优势,可以同时选择多个!,下面分别做几个演示!! < br /> < br /> 属性列表 < table align ="center" border ="1" cellpadding ="0" cellspacing ="0" width ="600" > < tr > < td style ="width: 118px" > SelectionMode </ td > < td > 组件中条目的选择的类型即:多选、单选。Single,Multiple </ td > </ tr > < tr > < td style ="width: 118px" > Rows </ td > < td > 此组件显示总共多少行 </ td > </ tr > < tr > < td style ="width: 118px" > Selected </ td > < td > 检测条目十分被选中 </ td > </ tr > < tr > < td style ="width: 118px" > SelectedItem </ td > < td > 返回的类型是ListItem,获得组件中被选择的条目 </ td > </ tr > < tr > < td style ="width: 118px" > Count </ td > < td > 组件中条目的总数 </ td > </ tr > < tr > < td style ="width: 118px" > SelectedIndex </ td > < td > 组件中被选择的条目的索引值 </ td > </ tr > < tr > < td style ="width: 118px" > Items </ td > < td > 泛指组件中所有的条目,每一个条目的类型都是ListItem </ td > </ tr > </ table > < br /> < br /> < br /> < strong > 演示一: 响应列表框改变的事件 < br /> </ strong > < br /> 有两个事件,如果选了 洪川医药的话,那就转道 hc115.com去,否则就直接显示文字 < br /> < asp:ListBox ID ="ListBox1" runat ="server" AutoPostBack ="True" OnSelectedIndexChanged ="ListBox1_SelectedIndexChanged" > < asp:ListItem > 洪川医药 </ asp:ListItem > < asp:ListItem > 博客园 </ asp:ListItem > < asp:ListItem > 田洪川 </ asp:ListItem > < asp:ListItem > 天轰穿 </ asp:ListItem > </ asp:ListBox > < asp:Label ID ="Label1" runat ="server" ></ asp:Label >< br /> < br /> < strong > 演示二: 动态添加列表框中的项,并且移出指定项 < br /> </ strong > < br /> < asp:ListBox ID ="ListBox2" runat ="server" ></ asp:ListBox > < asp:TextBox ID ="TextBox1" runat ="server" AutoPostBack ="True" OnTextChanged ="TextBox1_TextChanged" Width ="78px" ></ asp:TextBox > 当你在前面文本框输入了内容后就在其他任意地方点一下 < br /> < asp:Button ID ="Button1" runat ="server" OnClick ="Button1_Click" Text ="移除" /> < asp:Button ID ="Button2" runat ="server" OnClick ="Button2_Click" Text ="清空" />< br /> < br /> < strong > 演示三 : 列表框里的值可以一次选择多个 < br /> </ strong > < br /> 其实这个没有什么复杂的,就是把列表框的 SelectionMode="Multiple" 就可以了,正常情况下是等于Single的,就是只能选一行 < br /> 按着CTRL键,可以多选 < br /> < asp:ListBox ID ="ListBox3" runat ="server" OnSelectedIndexChanged ="ListBox1_SelectedIndexChanged" SelectionMode ="Multiple" > < asp:ListItem > 洪川医药 </ asp:ListItem > < asp:ListItem > 博客园 </ asp:ListItem > < asp:ListItem > 田洪川 </ asp:ListItem > < asp:ListItem > 天轰穿 </ asp:ListItem > </ asp:ListBox > < br /> < br /> < strong > 演示四 ,两级联动菜单 < br /> </ strong > < br /> < asp:ListBox ID ="ListBox4" runat ="server" AutoPostBack ="True" OnSelectedIndexChanged ="ListBox4_SelectedIndexChanged" > < asp:ListItem > 洪川医药 </ asp:ListItem > < asp:ListItem > 天轰穿的博客 </ asp:ListItem > </ asp:ListBox > < asp:ListBox ID ="ListBox5" runat ="server" ></ asp:ListBox >< br /> < br /> < strong > 演示五 : 如何实现组件中的指定条目的移位和移动指针到指定位置 < br /> </ strong > < br /> 参考 < span style ="color: #3366ff" > 马金虎 写的 < WinForm中的ListBox组件编程 > < br /> 移位包括二种,其一是向上移位,其二是向下移位。程序中具体的实现思路是:创建一个ListItem对象,并把要移位指定的条目中的内容先暂放在此新建的这个对象中。如果选定的是向上移位,就把当前选定的条目的上一个条目的值赋值给当前选定的条目,然后把刚才新建的对象的值,再赋值给选定条目的上一个条目,完成条目的向上移位操作。对于向下移位,可以仿效上面的做法,但和上面做法的主要区别在于不是选定条目的上一个条目了,而是选定条目的下一个条目。 < br /> < br /> </ span >< asp:ListBox ID ="ListBox6" runat ="server" OnSelectedIndexChanged ="ListBox1_SelectedIndexChanged" Height ="106px" > < asp:ListItem Value ="1 洪川医药" > 1 洪川医药 </ asp:ListItem > < asp:ListItem Value ="2 博客园" > 2 博客园 </ asp:ListItem > < asp:ListItem Value ="3 田洪川" > 3 田洪川 </ asp:ListItem > < asp:ListItem Value ="4 天轰穿" > 4 天轰穿 </ asp:ListItem > < asp:ListItem Value ="5 黄小梅" > 5 黄小梅 </ asp:ListItem > < asp:ListItem Value ="6 田皓文" > 6 田皓文 </ asp:ListItem > </ asp:ListBox >< br /> 在一般编程中都应该判断列表中是否有数据,这里不用了,因为加上判断,代码太多了,没意思 < br /> < asp:Button ID ="Button7" runat ="server" CommandName ="up" OnClick ="Button7_Click" Text ="向上移动一位" Width ="134px" /> < asp:Button ID ="Button8" runat ="server" CommandName ="down" OnClick ="Button7_Click" Text ="向下移动一位" Width ="138px" />< br /> < asp:Button ID ="Button4" runat ="server" OnClick ="Button4_Click" Text ="至首条" Width ="70px" /> < asp:Button ID ="Button5" runat ="server" OnClick ="Button5_Click" Text ="上一条" Width ="68px" /> < asp:Button ID ="Button6" runat ="server" OnClick ="Button6_Click" Text ="下一条" Width ="68px" /> < asp:Button ID ="Button9" runat ="server" OnClick ="Button9_Click" Text ="至末尾" />< br /> < br /> < br /> </ div > </ form > </ body > </ html > using System; using System.Data; using System.Configuration; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; public partial class _Default : System.Web.UI.Page ... { protected void Page_Load(object sender, EventArgs e) ...{ } protected void ListBox1_SelectedIndexChanged(object sender, EventArgs e) ...{//演示一的事件 Label1.Text = ListBox1.SelectedValue.ToString();//把Label的文字赋值为列表框中被选中的值 if (ListBox1.SelectedValue == "洪川医药")//当选定项的值等于 洪川医药的时候就转到hc115.com 去 ...{ Response.Redirect("http://www.hc115.com/qyml/"); } } protected void TextBox1_TextChanged(object sender, EventArgs e) ...{//懒得用按纽了,演示二的添加列表项动作 ListBox2.Items.Add(TextBox1.Text);//给列表项添加一项 Button1.Text = "移除";//防止有人先在点击了移除按纽,所以下面做了处理,这里如果是添加了,当然按纽就还原了撒 Button1.Enabled = true; Button2.Text = "清空";//防止有人先在点击了移除按纽,所以下面做了处理,这里如果是添加了,当然按纽就还原了撒 Button2.Enabled = true; } protected void Button1_Click(object sender, EventArgs e) ...{//移出事件 if (ListBox2.Items.Count > 0)//如果列表项的索引值大与0 ...{ ListBox2.Items.Remove(ListBox2.SelectedItem);//就移除指定项 } else ...{//否则 Button1.Text = "你疯了哇,都没有东西,你叫我删什么,有本事你自己去删嘛"; Button1.Enabled = false; } } protected void Button2_Click(object sender, EventArgs e) ...{//原理基本同上 if (ListBox2.Items.Count > 0)//如果列表项的索引值大与0 ...{ ListBox2.Items.Clear();//就清空所有项 } else ...{//否则 Button2.Text = "你疯了哇,都没有东西,你叫我清空什么,有本事你自己去清空嘛"; Button2.Enabled = false; } } protected void ListBox4_SelectedIndexChanged(object sender, EventArgs e) ...{//两级联动菜单事件 switch (ListBox4.SelectedValue)//判断一级列表中被选中的值 ...{ case "洪川医药"://如果是这个,那二级就添加下面这些 ListBox5.Items.Add("医院评价"); ListBox5.Items.Add("医院名录"); ListBox5.Items.Add("假药暴光"); ListBox5.Items.Add("医药黑幕"); break; case "天轰穿的博客"://如果是这个,那二级就添加下面这些 ListBox5.Items.Add("Vs2005系列控件"); ListBox5.Items.Add("学习笔记"); break; } } protected void Button7_Click(object sender, EventArgs e) ...{//向上下移动一条 事件 if (((Button)sender).CommandName == "up" && ListBox6.SelectedIndex > 0 || ((Button)sender).CommandName == "down" && ListBox6.SelectedIndex < ListBox6.Items.Count - 1) ...{ //判断传来的命令名必须是 up并且所选条目的索引必须大于0 或者 down并且所选条目必须小于最大项 int index;//为了减少代码,这里做一个对变量的判断,以后就直接调用变量, if (((Button)sender).CommandName == "up") ...{ index = -1;//以后的索引本来就是在当前的条目上加一或者减,所以这个方法很不错 } else ...{ index = 1; } ListItem lt = new ListItem(ListBox6.SelectedItem.Text,ListBox6.SelectedValue);//将当前条目的文本以及值都保存到一个临时变量里面 ListBox6.Items[ListBox6.SelectedIndex].Text = ListBox6.Items[ListBox6.SelectedIndex + index].Text;//被选中项的值等于上一条或者下一条的值 ListBox6.Items[ListBox6.SelectedIndex].Value = ListBox6.Items[ListBox6.SelectedIndex + index].Value;//被选中项的值等于上一条或者下一条的值 ListBox6.Items[ListBox6.SelectedIndex + index].Text = lt.Text;//把被选中项的上一条或者下一条的值用临时变量中的取代 ListBox6.Items[ListBox6.SelectedIndex + index].Value = lt.Value;//把被选中项的上一条或者下一条的值用临时变量中的取代 ListBox6.SelectedIndex = ListBox6.SelectedIndex + index;//把鼠标指针放到移动后的那条上 } } protected void Button4_Click(object sender, EventArgs e) ...{//移至首条 ListBox6.SelectedIndex = 0;//将被选中项的索引设置为0就可以啦, } protected void Button9_Click(object sender, EventArgs e) ...{//移至尾条 ListBox6.SelectedIndex = ListBox6.Items.Count-1;//因为C#里面默认的索引都是从0开始,所以最大项必须减一才是真实的 } protected void Button5_Click(object sender, EventArgs e) ...{//上一条 ListBox6.SelectedIndex = ListBox6.SelectedIndex - 1;//用当前被选中的索引去减一 } protected void Button6_Click(object sender, EventArgs e) ...{//下一条 ListBox6.SelectedIndex = ListBox6.SelectedIndex + 1;//用当前被选中的索引去加一 }}