C#中RadioButton分组,和RadioButtonList使用

http://msdn.microsoft.com/library/en-us/cpref/html/frlrfSystemWindowsFormsGroupBoxClassTopic.asp?frame=true

如果要对RadioButton控件进行分组,就要使用到其GroupName属性,GroupName值相同的控件被认为是一组的,在同一组RadioButton控件中你始终只能选择一项,但RadioButton控件的分组属性GroupName似乎也仅仅是起分组的作用而已,对获取选中项的值一点帮助都没有(个人观点),而使用RadioButtonList似乎是更好的方案,同一个RadioButtonList的选项自然被认为是一组,并且获取选中项的值也比RadioButton好多了。

<asp:RadioButtonList runat="server" ID="RadioButtonList1" BorderStyle="None" 
                        RepeatLayout="Flow">
                        <asp:ListItem>工作日加班</asp:ListItem>
                        <asp:ListItem>周末加班</asp:ListItem>
                        <asp:ListItem>法定节日加班</asp:ListItem>
                        </asp:RadioButtonList>

  “布局”里建议把RepeatLayout改为:Flow

否则默认是一个table,要写一个class把table里的td去掉

	.radioButtonList{border-style:none;}
       	.radioButtonList tr td{border-style:none;}

  

判断取值和 DropDownList一样,挺方便的。

if(RadioButtonList1.SelectedIndex == 0){}

RadioButtonList1.SelectedItem.Text.Trim();

如果想让后台代码默认选中,就在后台Page_Load()里面加

this.RadioButtonList1.SelectedIndex = 0;

  


posted @ 2011-10-18 16:11  becket  阅读(51313)  评论(0编辑  收藏  举报