博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
教学思路ASP.Net之服务器控件:三、DropDownList、ListBox、CheckBoxList、RadioButtonList控件...
阅读量:7091 次
发布时间:2019-06-28

本文共 3876 字,大约阅读时间需要 12 分钟。

 DropDownList控件类似于Winform中的combox下拉控件,如果想手工的添加DropDownList中的项目ListItem的方式如图:
      选中编辑项后出现ListItem集合编辑器,通过添加按钮添加一个listItem项,同时我们会发现每项都会有四个属性需要设置,Enable设置此项目是否可见;当Selected属性=true时,页面加载时DropDownList控件做选中的改项目,如果所有的项目的Selected的属性都是False时,页面加载后会默认选中第一个项目,这点与combox不同,combox在Winform中界面加载时,如果不设置选中的项,文本框内将没有任何一项的Text;Text属性设置了项目的显示文本;Value属性设置了此控件上每个项的Value值,也就是说每一项可以绑定两个数值,Text属性将显示在控件上,下面这段代码是页面的源代码:
1                                        
<
asp:DropDownList 
ID
="DropDownList1" 
runat
="server" 
> 

2                                                 
<
asp:ListItem 
Enabled
="False" 
Value
="0"
>选项1
</
asp:ListItem
> 

3                                                 
<
asp:ListItem 
Selected
="True" 
Value
="1"
>选项2
</
asp:ListItem
> 

4                                                 
<
asp:ListItem 
Value
="2"
>选项3
</
asp:ListItem
> 

5                                         
</
asp:DropDownList
>
        选项1将不会显示出来,因为Enable=“False”,页面加载将显示选项2,因为Selected="True",下面我们在页面中加入一个Button,点击Button进入Button的Click事件订阅的方法,写如下代码:
InBlock.gif1     
protected 
void Button1_Click(
object sender, EventArgs e) 

InBlock.gif2         { 

InBlock.gif3                 Response.Write(DropDownList1.SelectedItem.Text+
"<br/>"); 

InBlock.gif4                 Response.Write(DropDownList1.SelectedItem.Value + 
"<br/>"); 

InBlock.gif5                 Response.Write(DropDownList1 .SelectedValue); 

InBlock.gif6         } 

页面加载后直接点击Button1页面出现的结果为:
选项2
1
1
        从结果可以看出页面加载时选中了选项2,所以点击Button1时通过选中的项的相关属性能得到绑定在项上的Text和Value的值。
      CheckListBox、RadioButtonList、listBox添加项ListItem的方式如上。
       下面是对
CheckListBox的布置,同样也是添加3个选项:
1                                        
<
asp:CheckBoxList 
ID
="CheckBoxList1" 
runat
="server"
> 

2                                                 
<
asp:ListItem 
Value
="0"
>选项1
</
asp:ListItem
> 

3                                                 
<
asp:ListItem 
Value
="1"
>选项2
</
asp:ListItem
> 

4                                                 
<
asp:ListItem 
Value
="2"
>选项3
</
asp:ListItem
> 

5                                         
</
asp:CheckBoxList
>
          在页面中加入一个Button2,当选中选项2和3时,点击Button2在页面中显示出选中的选项文本,代码如下:
InBlock.gif 1    
protected 
void Button2_Click(
object sender, EventArgs e) 

InBlock.gif 2         { 

InBlock.gif 3                 
foreach (ListItem xm 
in CheckBoxList1.Items) 

InBlock.gif 4                 {    

InBlock.gif 5                         
if(xm.Selected) 

InBlock.gif 6                                Response.Write(
"你选的是:"+xm.Text+
"<br/>"); 

InBlock.gif 7                 } 

InBlock.gif 8    

InBlock.gif 9                 
//for (int i = 0; i < CheckBoxList1.Items.Count; i++) 

InBlock.gif10                 
//{ 

InBlock.gif11                 
//        if (CheckBoxList1.Items[i].Selected) 

InBlock.gif12                 
//                Response.Write("你选的是:" + CheckBoxList1.Items[i].Text + "<br>"); 

InBlock.gif13                 
//} 

InBlock.gif14         }
        上面的代码中用了两种循环将CheckListBox的每个项遍历出来,然后利用判断语句判断该项是否被选中,从而得到输出结果。
显示的结果为
你选的是:选项2
你选的是:选项3
        
ListBox控件的选中方式中比DropDownList控件相比,支持选中多项,但是需要调整SelectionMode属性为Single只能选中一项,当属性值改为Multiple将支持选中多项,并可以使用Ctrl或Shift键,按常规方式选择多项,我们还是给listBox添加3个项,然后点击Button1后,在页面中显示用户选择的项文本。
<
asp:ListBox 
ID
="ListBox1" 
runat
="server" 
SelectionMode
="Multiple"
> 

2                                         
<
asp:ListItem 
Value
="0"
>选项1
</
asp:ListItem
> 

3                                                 
<
asp:ListItem 
Value
="1"
>选项2
</
asp:ListItem
> 

4                                                 
<
asp:ListItem 
Value
="2"
>选项3
</
asp:ListItem
> 

5                                         
</
asp:ListBox
>
选中listBox项的cs代码
InBlock.gif1    
protected 
void Button1_Click(
object sender, EventArgs e) 

InBlock.gif2         { 

InBlock.gif3                 
foreach (ListItem xm 
in ListBox1.Items) 

InBlock.gif4                         
if (xm.Selected) 

InBlock.gif5                                 Response.Write(
"你选中的是"+xm.ToString()+
"<br/>"); 

InBlock.gif6         } 

 
        
RadioButtonList控件是将若干个RadioButton组合在一起,控件本身保持每个选项的排他性,我们还是如一个图一样的设置RadioButton的三个选项,然后点击Button3,在页面上写出选中的单选项文本。
<
asp:RadioButtonList 
ID
="RadioButtonList1" 
runat
="server"
> 

2                                         
<
asp:ListItem 
Value
="0"
>选项1
</
asp:ListItem
> 

3                                                 
<
asp:ListItem 
Value
="1"
>选项2
</
asp:ListItem
> 

4                                                 
<
asp:ListItem 
Value
="2"
>选项3
</
asp:ListItem
> 

5                                         
</
asp:RadioButtonList
>
 
 点击Button3的cs代码为:
InBlock.gif
protected 
void Button3_Click(
object sender, EventArgs e) 

InBlock.gif        { 

InBlock.gif                Response.Write(
"你选的是:" + RadioButtonList1.SelectedItem.ToString()+ 
"<br/>"); 

InBlock.gif                
//Response.Write("你选的是:" + RadioButtonList1.SelectedItem.Text+ "<br/>"); 

InBlock.gif}
 
       这两行语句都能得到选中的单选按钮的文本内容,
      下面请大家添加一个Button4和RadioButtonList2,然后当点击Button4时,RadioButtonList2在页面上将自动加载项目,内容为:初始年、第1年、第2年、……、第5年,共6个选项,并默认选中初始年。
InBlock.gif 1            
protected 
void Button4_Click(
object sender, EventArgs e) 

InBlock.gif 2        { 

InBlock.gif 3                ListItem li = 
new ListItem(); 

InBlock.gif 4                li.Text = 
"初始年"

InBlock.gif 5                RadioButtonList2.Items.Add(li); 

InBlock.gif 6                RadioButtonList2.Items[0].Selected = 
true

InBlock.gif 7                
for (
int i = 1; i <= 5; i++) 

InBlock.gif 8                { 

InBlock.gif 9                        li = 
new ListItem(); 

InBlock.gif10                        li.Text = 
"第"+i+
"年"

InBlock.gif11                        RadioButtonList2.Items.Add(li); 

InBlock.gif12                } 

InBlock.gif13        }
 
       RadioButtonList2的Items集合中应加入ListItem类型的对象Li,并且利用for循环,将 RadioButtonList2控件中加入年份。
       从上面的例题我们会发现,向这些控件的项集合Items中加入一个项ListBoxItem使用Add方法,移除项还是使用Remove(项值)或RemoveAt(索引),这些用法与Winform中完全一致,此处就不做详解了。
本文转自叶子文文博客51CTO博客,原文链接http://blog.51cto.com/leafwf/185685如需转载请自行联系原作者
叶子文文
你可能感兴趣的文章
HDU4300 Clairewd’s message
查看>>
【原】iOS学习之图片拉伸处理(类似qq的气泡)
查看>>
postman抓包
查看>>
最大稳定极值区域(MSER)检测
查看>>
如何解决CorelDRAW中尖突问题
查看>>
Javascript实现Web颜色值转换
查看>>
拼接日期填写表单
查看>>
工控系统安全问题汇总(一)
查看>>
4、SpringBoot------邮件发送(2)
查看>>
java创建二叉树并递归遍历二叉树
查看>>
JSON必知必会
查看>>
安全站点导航
查看>>
Oracle Job
查看>>
收集一些有意思的ASCII程序注释(持续收集中,希望大家踊跃贡献)
查看>>
做网站的各种推荐网
查看>>
leetcode 10. 正则表达式匹配
查看>>
JM8.6中帧内帧间模式的选择
查看>>
测试覆盖率工具:EclEmma
查看>>
《CLR via C#》读书笔记 之 基元类型、引用类型和值类型
查看>>
BOS常用代码说明
查看>>