1. 属性列表:

SelectionMode    组件中条目的选择类型,即多选(Multiple)、单选(Single)
    Rows             列表框中显示总共多少行
    Selected         检测条目是否被选中
    SelectedItem     返回的类型是ListItem,获得列表框中被选择的条目
    Count            列表框中条目的总数
    SelectedIndex    列表框中被选择项的索引值
    Items            泛指列表框中的所有项,每一项的类型都是ListItem

2. 取列表框中被选中的值

ListBox.SelectedValue

3. 动态的添加列表框中的项:

ListBox.Items.Add("所要添加的项");

4. 移出指定项:

//首先判断列表框中的项是否大于0
     If(ListBox.Items.Count > 0 )
     {
//移出选择的项
ListBox.Items.Remove(ListBox.SelectedItem);
     }

5. 清空所有项:

//首先判断列表框中的项是否大于0
     If(ListBox.Items.Count > 0 )
     {
//清空所有项
ListBox.Items.Clear();
     }

6. 列表框可以一次选择多项:
   
     只需设置列表框的属性 SelectionMode="Multiple",按Ctrl可以多选

7. 两个列表框联动,即两级联动菜单

//判断第一个列表框中被选中的值
     switch(ListBox1.SelectValue)
     {
//如果是"A",第二个列表框中就添加这些:
case "A"
      ListBox2.Items.Clear();
      ListBox2.Items.Add("A1");
      ListBox2.Items.Add("A2");
      ListBox2.Items.Add("A3");
//如果是"B",第二个列表框中就添加这些:
case "B"
      ListBox2.Items.Clear();
      ListBox2.Items.Add("B1");
      ListBox2.Items.Add("B2");
      ListBox2.Items.Add("B3");
     }

8. 实现列表框中项的移位
     即:向上移位、向下移位
     具体的思路为:创建一个ListBox对象,并把要移位的项先暂放在这个对象中。
     如果是向上移位,就是把当前选定项的的上一项的值赋给当前选定的项,然后
     把刚才新加入的对象的值,再附给当前选定项的前一项。
     具体代码为:
      //定义一个变量,作移位用
      index = -1;
      //将当前条目的文本以及值都保存到一个临时变量里面
      ListItem lt=new ListItem (ListBox.SelectedItem.Text,ListBox.SelectedValue);
      //被选中的项的值等于上一条或下一条的值
      ListBox.Items[ListBox.SelectedIndex].Text=ListBox.Items[ListBox.SelectedIndex + index].Text;
      //被选中的项的值等于上一条或下一条的值
      ListBox.Items[ListBox.SelectedIndex].Value=ListBox.Items[ListBox.SelectedIndex + index].Value;
      //把被选中项的前一条或下一条的值用临时变量中的取代
      ListBox.Items[ListBox.SelectedIndex].Test=lt.Test;
      //把被选中项的前一条或下一条的值用临时变量中的取代
      ListBox.Items[ListBox.SelectedIndex].Value=lt.Value;
      //把鼠标指针放到移动后的那项上
      ListBox.Items[ListBox.SelectedIndex].Value=lt.Value;

9. 移动指针到指定位置:

(1).移至首条
          //将被选中项的索引设置为0就OK了
          ListBox.SelectIndex=0;
      (2).移至尾条
          //将被选中项的索引设置为ListBox.Items.Count-1就OK了
          ListBox.SelectIndex=ListBox.Items.Count-1;
      (3).上一条
          //用当前被选中的索引去减 1
          ListBox.SelectIndex=ListBox.SelectIndex - 1;
      (4).下一条
          //用当前被选中的索引去加 1
          ListBox.SelectIndex=ListBox.SelectIndex + 1;

出自 51CTO.COM博客

this.ListBox1.Items.Insertat(3,new   ListItem("插入在第3行之后项",""));

this.ListBox1.Items.Insertat(index,ListItem)

ListBox1.Items.Insert(0,new   ListItem("text","value"));

ASP.NET中添加控件ListBox , 属性设为 Multiple , 则可进行多选.
就以两个listbox之间多选添加项目为例.
两个控件为listboxleft , listboxright 定义了一个动态数组用于中间存储 arrRight .具体代码如下:

//读取右边选中项目
   ArrayList arrRight = new ArrayList();
   foreach(ListItem item in this.ListBoxRight.Items) //按类型listitem读取listbox中选定项
   {
    if(item.Selected) //判断是否选中
    {
     arrRight.Add(item);
    }
   }

//右边移除选定项目 左边添加
   foreach(ListItem item in arrRight)
   {
    this.ListBoxLeft.Items.Add(item);
    this.ListBoxRight.Items.Remove(item);
   }
不能将item的添加删除直接写在if(item.Selected){}内,因为项目remove后会出现错误

Move the Item of ListBox

.aspx

--------------------------------

<asp:ListBox id="ListBox1" style="Z-INDEX: 105; LEFT: 96px; POSITION: absolute; TOP: 344px" runat="server">
    <asp:ListItem Value="a1">a1</asp:ListItem>
    <asp:ListItem Value="a2">a2</asp:ListItem>
    <asp:ListItem Value="a3">a3</asp:ListItem>
   </asp:ListBox>

----------------------------------

.cs

//for 个

private void Button1_Click(object sender, System.EventArgs e){
   if (this.ListBox1.SelectedIndex>=0)
   {
    int i = ListBox1.SelectedIndex;
    if(i>0)
    {
     string values = this.ListBox1.Items[i].Text ;
     this.ListBox1.Items[i].Text = this.ListBox1.Items[i-1].Text ;
     this.ListBox1.Items[i-1].Text = values;
    }
   }

}

ASP.NET中ListBox实现Double Click事件

2007-06-24 13:18

在ASP.NET中的ListBox控件的使用中很多人都发现没有了WINFORM中ListBox的鼠标双击事件

这样就给我们开发带来很多不方便的地方

也看了很多CSDN上用JS来实现双击事件的方法,都是不完整的,最后发现以下方法是最有效的

首先在WEB页面上加入JS脚本和存放ListBox事件的隐藏输入框
再将ASP.NET控件ListBox中加入双击事件声明
<html>

<head>

<script language="javascript">

function ListBox1_DoubleClick() {

document.forms[0].ListBox1Hidden.value = "doubleclicked";

document.forms[0].submit();

}

</script>

</head>

<body>

<form runat="server">

<div>Double click on Listbox

<br />

<asp:ListBox id="ListBox1"

ondblclick="ListBox1_DoubleClick()" runat="server">

<asp:ListItem Value="1">One</asp:ListItem>

<asp:ListItem Value="2">Two</asp:ListItem>

<asp:ListItem Value="3">Three</asp:ListItem>

<asp:ListItem Value="4">Four</asp:ListItem>

</asp:ListBox>

<input type="hidden" name="ListBox1Hidden" />

</div>

<div>click on button

<br />

<asp:Button id="Button1" onclick="Button1_Click"

runat="server" Text="Button"/>

</div>

</form>

</body>

</html>

最后在WEB窗体加载时候执行下列代码就能实现双击ListBox中Item执行一些操作

void Page_Load(Object sender, EventArgs e){

if(Request.Params["ListBox1Hidden"] != null

&& (string)Request.Params["ListBox1Hidden"] == "doubleclicked" {

//This means It was double click

Response.Write("Double Click was fired selected item is "

+ ListBox1.SelectedItem.Text);

//可以在这里加要运行的代码

}

}

listbox控件使用的更多相关文章

  1. asp.net中的ListBox控件添加双击事件

    问题:在Aspx页里的ListBox A中添加双击事件,将选中项添加到另一个ListBox B中,双击ListBox B中的选中项,删除当前选中项 页面: <asp:ListBox ID=&qu ...

  2. WPF中ListBox控件在选择模式(SelectionMode)为Single时仍然出现多个Item被选中的问题

    最近在学习WPF过程中使用到了ListBox控件,在使用时遇到下面的奇怪问题: 代码如下: listBox.Items.Add("绘图"); listBox.Items.Add(& ...

  3. MATLAB GUI程序设计中ListBox控件在运行期间消失的原因及解决方法

    在运行期间,ListBox控件突然消失,同时给出如下错误提示: Warning: single-selection listbox control requires that Value be an ...

  4. MFC中Listbox控件的简单使用

    MFC中listbox控件是为了显示一系列的文本,每个文本占一行.   Listbox控件可以设置属性为: LBS_CHILD   :(默认)子窗口 LBS_Visible :(默认)可视 LBS_M ...

  5. 异步方式向WPF ListBox控件中一条一条添加记录

    向ListBox绑定数据源时,如果数据量过大,可能会使得程序卡死,这是就需要一条一条的向ListBox的数据源中添加记录了,下面是个小Demo: 1.前台代码,就是一个ListBox控件 <Wi ...

  6. asp.net Listbox控件用法

    2008-02-18 19:56 来源: 作者: ListBox(列表框)控件可以显示一组项目的列表,用户可以根据需要从中选择一个或多个选项.列表框可以为用户提供所有选项的列表.虽然也可设置列表框为多 ...

  7. ListBox控件

    主要介绍:自定义数据.绑定数据库数据 前台代码: <div> <asp:ListBox ID=" Width ="100px"> <asp: ...

  8. ASP.NET中ListBox控件的使用

    文章来源:http://www.cnblogs.com/fengzheng126/archive/2012/04/10/2441551.html ListBox控件属性介绍: SelectIndex: ...

  9. c#控件攻略宝典之ListBox控件

    ListBox控件的使用: 1)控件属性 Items SelectedItems SelectioModes 2)数据绑定 DataSoure DisplayMember ValueMenber 3) ...

随机推荐

  1. c# winform 窗体之间的传参

    说起winform程序中窗体之间的参数互传,大家找度娘会找到很多方法: 1.在窗体类中创建全局变量,类型为公开.静态的: 2.在窗体类中定义狗仔函数: 3.通过实践来船体参数: 这三种思路完全来自于霖 ...

  2. impala基础

    impala: 查询impala表时一定要加库名使用级联删除带有表的数据库:DROP database name cascade; insert插入的两种方式: 1. insert into empl ...

  3. Java中的回调

    又忙了一周,事情差不多解决了,终于有可以继续写我的博客了(各位看官久等了). 这次我们来谈一谈Java里的一个很有意思的东西--回调. 什么叫回调,一本正经的来讲,在计算机程序设计中,回调函数是指通过 ...

  4. js限制日期选择范围是两个月

    $(".dateInputClass input:eq(0)").bind("click", function(){WdatePicker({dateFmt:' ...

  5. java系列视频教程下载

    1.马士兵J2SE基础录屏视频 珍藏版 链接:https://pan.baidu.com/s/1eRMJqkq    密码:qa66 2.spring视频教程 链接:https://pan.baidu ...

  6. Python中的冒泡排序

    冒泡排序 冒泡排序(英语:Bubble Sort)是一种简单的排序算法.它重复地遍历要排序的数列,一次比较两个元素,如果他们的顺序错误就把他们交换过来.遍历数列的工作是重复地进行直到没有再需要交换,也 ...

  7. mysql如何查看索引使用情况以及优化 - guols0612

    mysql中支持hash和btree索引.innodb和myisam只支持btree索引,而memory和heap存储引擎可以支持hash和btree索引 我们可以通过下面语句查询当前索引使用情况: ...

  8. 分享一些JAVA相关资源

    前言 以前在学习JAVA时,因为搜索相关资源过于不便,所以在搜集了一些好用的资源之后,将此分享. 文档主要包括面试文档, JAVA的技术文档(如JAVA并发实战.设计模式之类),LINUX的相关文档以 ...

  9. Express4.x API (二):Request (译)

    写在前面 最近学习express想要系统的过一遍API,www.expressjs.com是express英文官网(进入www.epxressjs.com.cn发现也是只有前几句话是中文呀~~),所以 ...

  10. LintCode-买卖股票的最佳时机

    如果有一个数组,它的第i个元素是一支给定的股票在第i天的价格.如果你最多仅仅同意完毕一次交易(比如,一次买卖股票),设计一个算法来找出最大利润. 您在真实的面试中是否遇到过这个题? Yes 例子 给出 ...