标签:
单选框:RadioButton RadioButton中有个属性GroupName,当单选时,必须把各个选项的组名设成同一个 查看按钮是否选中:bool sex = RadioButton1.Checked; Label1.Text = sex.ToString(); 单选按钮列表:RadioButtonList 属性:RepeatDirection:横向或纵向 绑定数据: TextDataContext context = new TextDataContext(); RadioButtonList1.DataSource = context.Nation; RadioButtonList1.DataTextField = "Name"; RadioButtonList1.DataValueField = "Code"; RadioButtonList1.DataBind(); 取选中项的值: Label1.Text =RadioButtonList1.SelectedValue.ToString(); 设置哪一项被选中: RadioButtonList1.SelectedIndex = 2; 复选框:CheckBox 看一下该按钮是否选中:checkbox1.Checked; 其余和单选框一样 复选框列表:CheckBoxList 属性RepeatDirection:横向Horizontal或纵向Vertical 绑定数据: CheckBoxList1.DataSource = context.Nation; CheckBoxList1.DataTextField = "Name"; CheckBoxList1.DataValueField = "Code"; CheckBoxList1.DataBind(); 取选中项的值: Label1.Text = "";//清空 foreach( ListItem item in CheckBoxList1.Items) { if (item.Selected) { Label1.Text += item.Text; } } 设置哪一项被选中: 单项选中: CheckBoxList1.SelectedIndex = 2; 多项选中: foreach (ListItem item in CheckBoxList1.Items) { if (item.Text == "汉族" || item.Text == "满族") { item.Selected = true; } } 例子: 如果代码没有执行,很有可能是AutoPostBack没有设置为Ture
标签:
原文地址:http://www.cnblogs.com/mn-b/p/5062219.html