RadioButtonList 单选按钮列表
属性:RepeatColumns 用于布局项的列数(每一行的个数)
RepeatDirection 选择Vertical,纵向排列;选择Horizontal,横向排列。
RepeatLayout 选择Table,RadioButtonList用<table>布局;
选择Flow,RadioButtonList用<span>布局;
选择OrderedList时RepeatDirection 属性必须选择Vertical,RadioButtonList用<ol>有序排列布局;
选择UnorderedList时RepeatDirection 属性必须选择Vertical,RadioButtonList用<ul>无序排列布局;
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 protected void Page_Load(object sender, EventArgs e) 2 { 3 if (!IsPostBack) 4 { 5 List<Class1> clist = new List<Class1>(); 6 Class1 C1 = new Class1() { Code = "001", Name = "张店" }; 7 Class1 C2 = new Class1() { Code = "002", Name = "淄川" }; 8 Class1 C3 = new Class1() { Code = "003", Name = "博山" }; 9 Class1 C4 = new Class1() { Code = "004", Name = "临淄" }; 10 Class1 C5 = new Class1() { Code = "005", Name = "周村" }; 11 Class1 C6 = new Class1() { Code = "006", Name = "桓台" }; 12 Class1 C7 = new Class1() { Code = "007", Name = "高青" }; 13 Class1 C8 = new Class1() { Code = "008", Name = "沂源" }; 14 clist.Add(C1); 15 clist.Add(C2); 16 clist.Add(C3); 17 clist.Add(C4); 18 clist.Add(C5); 19 clist.Add(C6); 20 clist.Add(C7); 21 clist.Add(C8); 22 //绑定数据 23 RadioButtonList1.DataSource = clist; 24 RadioButtonList1.DataTextField = "Name"; 25 RadioButtonList1.DataValueField = "Code"; 26 RadioButtonList1.DataBind();//将绑定的数据调用到控件*很重要* 27 28 //设置默认选中项 29 RadioButtonList1.SelectedIndex = 0;//默认第一个 30 // RadioButtonList1.SelectedIndex = clist.Count - 1;//默认最后一个 31 //RadioButtonList1.SelectedValue = "002";//默认选中项的Code为002 32 33 //根据Name选择默认选中项 34 //foreach(ListItem li in RadioButtonList1.Items) 35 //{ 36 // if(li.Text=="桓台") 37 // { 38 // li.Selected = true; 39 // } 40 //} 41 } 42 Button1.Click += Button1_Click; 43 } 44 45 void Button1_Click(object sender, EventArgs e) 46 { 47 Label1.Text = RadioButtonList1.SelectedValue;//取出Value值 48 Label1.Text = RadioButtonList1.SelectedItem.Text;//取出Text值 49 }
1、绑定数据:
1 RadioButtonList1.DataSource = clist; 2 RadioButtonList1.DataTextField = "Name";//显 3 RadioButtonList1.DataValueField = "Code";//隐 4 RadioButtonList1.DataBind();//将绑定的数据调用到控件*很重要*
2、设置选中项:
1 //根据索引设置 2 RadioButtonList1.SelectedIndex = 0;//默认第一个 3 RadioButtonList1.SelectedIndex = clist.Count - 1;//默认最后一个 4 //根据Value值设置 5 RadioButtonList1.SelectedValue = "002";//默认选中项的Code为002 6 7 //根据Text值设置 8 foreach (ListItem li in RadioButtonList1.Items) 9 { 10 if (li.Text == "桓台") 11 { 12 li.Selected = true; 13 } 14 }
3、取出数据:
1 Label1.Text = RadioButtonList1.SelectedValue;//取出Value值 2 Label1.Text = RadioButtonList1.SelectedItem.Text;//取出Text值
3 Label1.Text = RadioButtonList1.SelectedItem.Selected.ToString();//获取一个值判定是否被选中
CheckBoxList:复选框列表
属性:RepeatColumn、RepeatDirection 和RepeatLayout 同RadioButtonList的使用方式相同;
1、绑定数据:
1 CheckBoxList1.DataSource = clist; 2 CheckBoxList1.DataTextField = "Name";//显 3 CheckBoxList1.DataValueField = "Code";//隐 4 CheckBoxList1.DataBind();//将绑定的数据调用到控件*很重要*
2、设置选中项:
foreach (ListItem li in CheckBoxList1.Items) { if (li.Text == "桓台") { li.Selected = true; } if (li.Text == "张店") { li.Selected = true; } }
3、取出数据:
//取一个数据:
Label1.Text = CheckBoxList1.SelectedItem.Text;
//取一堆数据:
foreach (ListItem li in CheckBoxList1.Items) { if (li.Selected == true) { Label1.Text += li.Text+","; } }
DropDownList:下拉列表
1、绑定数据
1 DropDownList1.DataSource = clist; 2 DropDownList1.DataTextField = "Name"; 3 DropDownList1.DataValueField = "Code"; 4 DropDownList1.DataBind();
2、设置选中项
1 //根据索引设置 2 DropDownList1.SelectedIndex = 0; 3 DropDownList1.SelectedIndex = clist.Count - 1;//默认最后一个 4 //根据Value值设置 5 DropDownList1.SelectedValue = "002";//默认选中项的Code为002 6 7 //根据Text值设置 8 foreach (ListItem li in DropDownList1.Items) 9 { 10 if (li.Text == "桓台") 11 { 12 li.Selected = true; 13 } 14 }
3、取出数据
1 Label1.Text = DropDownList1.SelectedValue;//取出Value值 2 Label1.Text = DropDownList1.SelectedItem.Text;//取出Text值 3 Label1.Text = DropDownList1.SelectedItem.Selected.ToString();//获取一个值判定是否被选中
ListBox 复选列表
属性: SelectionMode 列表选择模式: Single 单选;Multiple多选
绑定数据,设置选中项,取出数据方法同CheckBoxList相同。