WinForm中的ListBox组件编程(asp.net) _转载
2008-01-19 22:59
309 查看
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组件的语法如下:
在Web页面中执行上面的语句就可以产生一个名称为"MyListBox",包含若干条目的ListBox组件。
二. ListBox组件中常用的属性:
我们通过以下表格来说明ListBox组件的一些常用的属性:
三. 通过一个例子来掌握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属性来实现以上四种方式的。以下就是具体实现这四种方式的程序代码:
(4).如何实现组件中的指定条目的移位:
移位包括二种,其一是向上移位,其二是向下移位。程序中具体的实现思路是:创建一个ListItem对象,并把要移位指定的条目中的内容先暂放在此新建的这个对象中。如果选定的是向上移位,就把当前选定的条目的上一个条目的值赋值给当前选定的条目,然后把刚才新建的对象的值,再赋值给选定条目的上一个条目,完成条目的向上移位操作。对于向下移位,可以仿效上面的做法,但和上面做法的主要区别在于不是选定条目的上一个条目了,而是选定条目的下一个条目。下列语句就是就是实现这种思路的具体的程序代码:
四. 本文中源程序代码(listbox.aspx)和执行的界面:
下图是执行了下列源程序代码(listbox.aspx)后,生成的界面:
listbox.aspx源程序代码,具体如下:
五. 总结:
本文主要介绍了WinForm组件中的一个比较重要的组件--ListBox的属性,以及其的主要的使用方法,掌握这些WinForm组件编程是进行.Net方面编程的首要条件,也是一个比较重要的环节。
一. 如何在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 > |
二. 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 ; } } |
移位包括二种,其一是向上移位,其二是向下移位。程序中具体的实现思路是:创建一个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)后,生成的界面:
<% @ 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的属性,以及其的主要的使用方法,掌握这些WinForm组件编程是进行.Net方面编程的首要条件,也是一个比较重要的环节。
相关文章推荐
- ASP.NET AJAX客户端编程之旅(六)——来做一个自己的客户端控件(转载)
- ASP.NET组件编程
- 【摘录】ASP.NET 2.0服务器控件与组件开发高级编程(第2章摘录1)
- ASP.NET组件编程step by step
- 作为ASP.NET开发人员必须养成的编程习惯(转载:51CTO)
- Asp.net Ajax 编程备忘录----细数28个服务器端控件 [转载]
- [转载]让asp.net默认的上传组件支持进度条反映
- asp.net最常用的三十三种编程代码(转载,同大家分享)
- ASP.NET底层与各个组件的初步认识与理解 (转载)
- 【转载】Asp.NET大文件上传组件开发总结(四)---封送数据给Asp.NET页面
- ASP.NET中WebForm组件CheckBoxList编程
- ASP.net组件编程中的两种事件编写方法
- ASP.net组件编程中的两种事件编写方法
- 读书片断之《ASP.NET 2.0 服务器控件与组件开发高级编程》
- ASP.NET AJAX客户端编程之旅(一)——Hello!ASP.NET AJAX(转载)
- ASP.NET组件编程step by step
- ASP.net组件编程中的两种事件编写方法
- ASP.NET AJAX客户端编程之旅(四)——以组件的思想开发Ajax应用:客户端组件初探
- ASP.net组件编程中的两种事件编写方法
- ASP.NET AJAX客户端编程之旅(五)——以组件的思想开发Ajax应用:行为、绑定及xml-script