ListBox组件是一个程序设计中经常使用到的组件,在Visual C#和Visual Basic .Net程序中使用这个组件,必须要在程序中导入.Net FrameWork SDK中名称空间System.Windows.Forms,因为在System.Windows.Forms名称空间中定义了这个组件。在ASP.NET的Web页面中,ListBox组件是作为一个服务器端组件的形式出现的,所谓服务器端组件就是这些组件是在服务器端存在的。本文就是来介绍ListBox组件在ASP.NET的Web页面中的具体使用和操作方法。

一.
如何在ASP.NET页面中定义一个ListBox组件:

在ASP.NET页面中创建一个ListBox组件的语法如下:

<asp:ListBox Id = "MyListBox" runat = "server" >

<asp:ListItem Value
= "1" >第一个条目</asp:ListItem >

<asp:ListItem Value = "2"
>第二个条目</asp:ListItem >

注释:这里还可以加入类似上面的若干条目

.....

</asp:ListBox >

在Web页面中执行上面的语句就可以产生一个名称为"MyListBox",包含若干条目的ListBox组件。

二. ListBox组件中常用的属性:

我们通过以下表格来说明ListBox组件的一些常用的属性:

属性名称 属性代表的意义
SelectionMode 组件中条目的选择的类型即:多选、单选。Single,Multiple
Rows 此组件显示总共多少行
Selected 检测条目十分被选中
SelectedItem 返回的类型是ListItem,获得组件中被选择的条目
Count 组件中条目的总数
SelectedIndex 组件中被选择的条目的索引值
Items 泛指组件中所有的条目,每一个条目的类型都是ListItem

三.
通过一个例子来掌握ListBox组件在ASP.NET页面中的具体用法:


在下面介绍ListBox组件在ASP.NET中的使用方法的时候,程序采用的程序设计语言是Visual C#

(1).如何在ListBox组件添加新的条目:

通过以下语句就可以在名称为lstItem的ListBox组件中增加一个名称为"Sample"的条目:

lstItem .
Items . Add ( new ListItem ( "Sample" ) )

(2).如何在ListBox组件中删除指定的条目:

下列语句就是删除名称为lstItem的ListBox组件中的选定的一个条目:

  lstItem . Items .
Remove ( lstItem . SelectedItem )

(3).如何在组件中移动指向条目的指针:

移动条目的指针主要有四种方式:至首条目、至尾条目、下一条、上一条。在程序设计中主要是通过操作组件的Count和SelectedIndex属性来实现以上四种方式的。以下就是具体实现这四种方式的程序代码:

//按钮"至首条"事件处理程序

if ( sender == First )

{

if (
lstItem . Items . Count > 0 )

{

lstItem . SelectedIndex = 0 ;

}

}

//按钮"至尾条"事件处理程序

if ( sender == Last )

{

if ( lstItem . Items . Count > 0 )

{

lstItem .
SelectedIndex = lstItem . Items . Count - 1 ;

}

}

//按钮"上一条"事件处理程序

if ( sender == Prev )

{

if (
lstItem . SelectedIndex > 0 )

{

lstItem . SelectedIndex =
lstItem . SelectedIndex - 1 ;

}

}

//按钮"下一条"事件处理程序

if ( sender == Next )

{

if ( lstItem . SelectedIndex <
lstItem . Items . Count - 1 )

{

lstItem . SelectedIndex =
lstItem . SelectedIndex + 1 ;

} }

(4).如何实现组件中的指定条目的移位:

移位包括二种,其一是向上移位,其二是向下移位。程序中具体的实现思路是:创建一个ListItem对象,并把要移位指定的条目中的内容先暂放在此新建的这个对象中。如果选定的是向上移位,就把当前选定的条目的上一个条目的值赋值给当前选定的条目,然后把刚才新建的对象的值,再赋值给选定条目的上一个条目,完成条目的向上移位操作。对于向下移位,可以仿效上面的做法,但和上面做法的主要区别在于不是选定条目的上一个条目了,而是选定条目的下一个条目。下列语句就是就是实现这种思路的具体的程序代码:

//按钮"向上移位"和"向下移位"事件处理程序

if ( ( sender == Up && lstItem .
SelectedIndex > 0 ) ||
    ( sender ==
Down && lstItem .
SelectedIndex < lstItem . Items . Count - 1 ) )

{

int offset ;

if ( sender == Up )

{

offset = -1 ;

}

else

{

offset = 1 ;

}

ListItem lstTemp =
new ListItem ( lstItem . SelectedItem . Text

                                lstItem . SelectedItem . Value ) ;

lstItem . Items [ lstItem.SelectedIndex ] .Text =
lstItem . Items [
lstItem . SelectedIndex + offset ] . Text ;

lstItem . Items [ lstItem .
SelectedIndex ] . Value =
lstItem . Items [ lstItem . SelectedIndex + offset
] . Value ;

lstItem . Items [ lstItem . SelectedIndex + offset ] . Text
=
lstTemp . Text ;

lstItem . Items [ lstItem . SelectedIndex +
offset ] . Value =
lstTemp . Value ;

lstItem . SelectedIndex =
lstItem . SelectedIndex + offset ;

}

四. 本文中源程序代码(listbox.aspx)和执行的界面:

下图是执行了下列源程序代码(listbox.aspx)后,生成的界面:

listbox.aspx源程序代码,具体如下:

<% @ Page Language = "C#" %>

<html >

<head >

<script runat = "server" >

protected void Button_Click ( object sender , EventArgs e )

{

//按钮"增加条目"事件处理程序

if ( sender == Add )

{

if (
txtItem.Text != "" )

{

lstItem . Items . Add ( new ListItem (
txtItem . Text ) ) ;

}

}

//按钮"删除"事件处理程序

if (
sender == Del )

{

if ( lstItem . SelectedIndex > -1 )

{

lstItem . Items . Remove ( lstItem . SelectedItem ) ;

}

}

//按钮"向上移位"和"向下移位"事件处理程序

if ( ( sender == Up &&
lstItem . SelectedIndex > 0 ) || ( sender == Down && lstItem .
SelectedIndex < lstItem . Items . Count - 1 ) )

{

int offset ;

if ( sender == Up )

{

offset = -1 ;

}

else

{

offset = 1 ;

}

ListItem lstTemp =
new ListItem ( lstItem . SelectedItem . Text , lstItem . SelectedItem . Value )
;

lstItem . Items [ lstItem.SelectedIndex ] .Text = lstItem . Items [
lstItem . SelectedIndex + offset ] . Text ;

lstItem . Items [ lstItem .
SelectedIndex ] . Value = lstItem . Items [ lstItem . SelectedIndex + offset ] .
Value ;

lstItem . Items [ lstItem . SelectedIndex + offset ] . Text =
lstTemp . Text ;

lstItem . Items [ lstItem . SelectedIndex + offset ] .
Value = lstTemp . Value ;

lstItem . SelectedIndex = lstItem .
SelectedIndex + offset ;

}

//按钮"至首条"事件处理程序

if ( sender
== First )

{

if ( lstItem . Items . Count > 0 )

{

lstItem . SelectedIndex = 0 ;

}

}

//按钮"至尾条"事件处理程序

if ( sender == Last )

{

if (
lstItem . Items . Count > 0 )

{

lstItem . SelectedIndex =
lstItem . Items . Count - 1 ;

}

}

//按钮"上一条"事件处理程序

if ( sender == Prev )

{

if ( lstItem . SelectedIndex > 0
)

{

lstItem . SelectedIndex = lstItem . SelectedIndex - 1 ;

}

}

//按钮"下一条"事件处理程序

if ( sender == Next )

{

if ( lstItem . SelectedIndex < lstItem . Items . Count - 1 )

{

lstItem . SelectedIndex = lstItem . SelectedIndex + 1 ;

}

}

}

</script >

</head >

<body

<form runat = "server" >

<table >

<tr > <td Colspan =
2 > <h1 > <font color = "red" > WinForm组件ListBox演示程序 </font > </h1 > </td> </tr

<tr >

<td > 请输入要增加的条目名称:</td >

<td >

<asp:TextBox id = "txtItem" TextMode = "SingleLine" runat = "server"/>

<asp:Button id = Add Text = "增加条目" runat = "server" onclick =
"Button_Click"/>

</td >

</tr >

<tr >

<td
>ListBox: <br >

<asp:ListBox id = "lstItem" Width = 200 Height = 250
runat = "server" >

<asp:ListItem > 第一个条目 </asp:ListItem >

</asp:ListBox >

</td >

<td >

<asp:Button id =
"Del" Text = "删除" runat = "server" onclick = "Button_Click" />

<asp:Button id = "Up" Text = "向上移位" runat = "server" onclick =
"Button_Click" />

<asp:Button id = "Down" Text = "向下移位" runat = "server"
onclick = "Button_Click" />

<asp:Button id = "First" Text = "至首条" runat
= "server" onclick = "Button_Click" />

<asp:Button id="Prev" Text =
"上一条" runat = "server" onclick = "Button_Click" />

<asp:Button id =
"Next" Text = "下一条" runat = "server" onclick = "Button_Click" />

<asp:Button id = "Last" Text = "至尾条" runat = "server" onclick =
"Button_Click" />

</td >

</tr >

</table >

</form

<body >

</html > 

WinForm中的ListBox组件编程的更多相关文章

  1. winform中的ListBox和ComboBox绑定数据

    将集合数据绑定到ListBox和ComboBox控件,界面上显示某个属性的内容 //... //自定义了Person类(有Name,Age,Heigth等属性) List<Person> ...

  2. WinForm中获取Listbox、DataGridView等控件某行对应的数据

    Listbox:listbox.SelectedItem as XXX DataGridView:dataGridView1.Rows[i].Cells[1].Value.ToString()

  3. [Winform]Media Player com组件应用中遇到的问题

    摘要 最近一个项目中,需要用到在客户端全屏循环播放视频,当时考虑使用开源的播放器,但控制起来不方便,然后考虑既然都是windows系统,那么可以考虑使用微软自带的Media Player播放器.所以在 ...

  4. 在C#中实现listbox的项上下移动(winform) 标准

      在C#中实现listbox的项上下移动(winform) 收藏人:梅毛子360   2013-10-02 | 阅:1  转:2  |  分享    |    来源              usi ...

  5. 浅析C#组件编程中的一些小细节

    控件与组件的区别(Control&Component的区别) 作者:作者不详  发布日期:2011-06-30 12:08:41 控件与组件的区别(Control&Component的 ...

  6. 分享在winform下实现模块化插件编程-优化版

    上一篇<分享在winform下实现模块化插件编程>已经实现了模块化编程,但我认为不够完美,存在以下几个问题: 1.IAppContext中的CreatePlugInForm方法只能依据完整 ...

  7. .Net中的反应式编程(Reactive Programming)

    系列主题:基于消息的软件架构模型演变 一.反应式编程(Reactive Programming) 1.什么是反应式编程:反应式编程(Reactive programming)简称Rx,他是一个使用LI ...

  8. Net中的反应式编程

    Net中的反应式编程(Reactive Programming)   系列主题:基于消息的软件架构模型演变 一.反应式编程(Reactive Programming) 1.什么是反应式编程:反应式编程 ...

  9. .NET中使用APlayer组件自制播放器

    目录 说明 APlayer介绍 APlayer具备功能 APlayer使用 自制播放器Demo 未完成工作 源码下载 说明 由于需求原因,需要在项目中(桌面程序)集成一个在线播放视频的功能.大概要具备 ...

随机推荐

  1. 图解TCP/IP笔记(2)——数据链路

    [转载请注明]https://www.cnblogs.com/igoslly/p/9396066.html ——终端节点之间的包传递 MAC寻址(物理寻址).介质共享.非公有网络.分组交换.环路检测. ...

  2. Python标准库sys

    1.命令行参数sys.argv 我们从Python语言之模块第一部分的例子开始,看看sys.argv中到底存了些什么内容. #Filename: using_sys.py import sys i=0 ...

  3. ajax请求参数的格式

    因为多写了一个contentType=“text/html”,请求的时候,参数总是转成了url&拼接的格式,导致请求不成功,调试了老半天 这个也是奇怪,为什么post只能接收json格式的数据 ...

  4. 网际协议IP简述

    最近花了些时间重新回顾了谢希仁教授主编的<计算机网络>关于网络层的章节,这是一本高校教材,里面关于计算机网络的内容比较基础,并且讲的很细致,笔者针对网际协议IP地址部分觉得有必要进行阅读后 ...

  5. H5 坑

    document.addEventListener("touchmove",function(e){e.preventDefault();},false); 防止滑动时整屏页面移动 ...

  6. [luoguP2342] 叠积木(并查集)

    传送门 up[i] 表示一个木块上面有多少个 all[i] 表示整个连通块内有多少个 那么 一个木块下面的木块个数为 all[root[i]] - up[i] - 1 注意:up[i] 可以在 fin ...

  7. hdu 3657 最大点权独立集变形(方格取数的变形最小割,对于最小割建图很好的题)

    转载:http://blog.csdn.net/cold__v__moon/article/details/7924269 /* 这道题和方格取数2相似,是在方格取数2的基础上的变形. 方格取数2解法 ...

  8. hdu_1060_Leftmost Digit_201311071827-2

    Leftmost Digit Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) T ...

  9. ZooKeeper配置文件常用配置项一览表(转)

     配置参数详解(主要是$ZOOKEEPER_HOME/conf/zoo.cfg文件) 参数名 说明 clientPort 客户端连接server的端口,即对外服务端口,一般设置为2181吧. data ...

  10. PHP包管理工具composer简单总结

    前言 接触laravel之后,才知道有PSR,composer之类的东西,PHP已经不再是一门草根语言了.最近在尝试玩thrift,需要安装PHP thrift依赖库,使用composer insta ...