教学思路ASP.Net之服务器控件:三、DropDownList、ListBox、CheckBoxList、RadioButtonList控件

简介:
 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如需转载请自行联系原作者

叶子文文
相关文章
|
7月前
|
开发框架 前端开发 .NET
Ajax之三 Ajax服务器端控件(下)
Ajax之三 Ajax服务器端控件(下)
45 0
|
4月前
|
开发框架 安全 .NET
IIS服务器发布ASP.NET项目
对于云服务器,程序员一般不会陌生,如果项目需要发布到现网,那么服务器是必不可缺的一项硬性条件,那么如何在云服务器上部署一个项目,需要做哪些配置准备,下面就由本文档为大家讲解,本篇以IIS服务器发布ASP.NET项目为例。
48 1
|
7月前
|
开发框架 前端开发 JavaScript
Ajax之三 Ajax服务器端控件(上)
Ajax之三 Ajax服务器端控件
35 0
|
3月前
|
SQL 开发框架 前端开发
ASP.NET WEB项目中GridView与Repeater数据绑定控件的用法
ASP.NET WEB项目中GridView与Repeater数据绑定控件的用法
34 0
|
4月前
|
SQL 开发框架 JavaScript
分享33个ASP.NET电子商务源码和40个ASP.NET控件组件源码,总有一款适合您
分享33个ASP.NET电子商务源码和40个ASP.NET控件组件源码,总有一款适合您
31 0
|
5月前
|
开发框架 .NET 数据安全/隐私保护
Asp.Net第二章服务器端控件
Asp.Net第二章服务器端控件
27 0
|
5月前
|
开发框架 JavaScript .NET
Asp.Net就业课之三验证控件
Asp.Net就业课之三验证控件
44 0
|
5月前
|
开发框架 .NET
Asp.Net就业课堂之模板控件
Asp.Net就业课堂之模板控件
40 1