C#中RadioButton分組,和RadioButtonList使用
C#中RadioButton分組,和RadioButtonList使用
如果要對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一樣,挺方便的。
如果想讓后臺代碼默認選中,就在后臺Page_Load()里面加
this.RadioButtonList1.SelectedIndex = 0; |