RadioButtonList 单选按钮列表
属性:RepeatColumns 用于布局项的列数(每一行的个数)
RepeatDirection 选择Vertical,纵向排列;选择Horizontal,横向排列。
RepeatLayout 选择Table,RadioButtonList用<table>布局;
选择Flow,RadioButtonList用<span>布局;
选择OrderedList时RepeatDirection 属性必须选择Vertical,RadioButtonList用<ol>有序排列布局;
选择UnorderedList时RepeatDirection 属性必须选择Vertical,RadioButtonList用<ul>无序排列布局;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
List<Class1> clist = new List<Class1>();
Class1 C1 = new Class1() { Code = "", Name = "张店" };
Class1 C2 = new Class1() { Code = "", Name = "淄川" };
Class1 C3 = new Class1() { Code = "", Name = "博山" };
Class1 C4 = new Class1() { Code = "", Name = "临淄" };
Class1 C5 = new Class1() { Code = "", Name = "周村" };
Class1 C6 = new Class1() { Code = "", Name = "桓台" };
Class1 C7 = new Class1() { Code = "", Name = "高青" };
Class1 C8 = new Class1() { Code = "", Name = "沂源" };
clist.Add(C1);
clist.Add(C2);
clist.Add(C3);
clist.Add(C4);
clist.Add(C5);
clist.Add(C6);
clist.Add(C7);
clist.Add(C8);
//绑定数据
RadioButtonList1.DataSource = clist;
RadioButtonList1.DataTextField = "Name";
RadioButtonList1.DataValueField = "Code";
RadioButtonList1.DataBind();//将绑定的数据调用到控件*很重要* //设置默认选中项
RadioButtonList1.SelectedIndex = ;//默认第一个
// RadioButtonList1.SelectedIndex = clist.Count - 1;//默认最后一个
//RadioButtonList1.SelectedValue = "002";//默认选中项的Code为002 //根据Name选择默认选中项
//foreach(ListItem li in RadioButtonList1.Items)
//{
// if(li.Text=="桓台")
// {
// li.Selected = true;
// }
//}
}
Button1.Click += Button1_Click;
} void Button1_Click(object sender, EventArgs e)
{
Label1.Text = RadioButtonList1.SelectedValue;//取出Value值
Label1.Text = RadioButtonList1.SelectedItem.Text;//取出Text值
}
1、绑定数据:
RadioButtonList1.DataSource = clist;
RadioButtonList1.DataTextField = "Name";//显
RadioButtonList1.DataValueField = "Code";//隐
RadioButtonList1.DataBind();//将绑定的数据调用到控件*很重要*
2、设置选中项:
//根据索引设置
RadioButtonList1.SelectedIndex = ;//默认第一个
RadioButtonList1.SelectedIndex = clist.Count - ;//默认最后一个
//根据Value值设置
RadioButtonList1.SelectedValue = "";//默认选中项的Code为002 //根据Text值设置
foreach (ListItem li in RadioButtonList1.Items)
{
if (li.Text == "桓台")
{
li.Selected = true;
}
}
3、取出数据:
Label1.Text = RadioButtonList1.SelectedValue;//取出Value值
Label1.Text = RadioButtonList1.SelectedItem.Text;//取出Text值
3 Label1.Text = RadioButtonList1.SelectedItem.Selected.ToString();//获取一个值判定是否被选中
CheckBoxList:复选框列表
属性:RepeatColumn、RepeatDirection 和RepeatLayout 同RadioButtonList的使用方式相同;
1、绑定数据:
CheckBoxList1.DataSource = clist;
CheckBoxList1.DataTextField = "Name";//显
CheckBoxList1.DataValueField = "Code";//隐
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、绑定数据
DropDownList1.DataSource = clist;
DropDownList1.DataTextField = "Name";
DropDownList1.DataValueField = "Code";
DropDownList1.DataBind();
2、设置选中项
//根据索引设置
DropDownList1.SelectedIndex = ;
DropDownList1.SelectedIndex = clist.Count - ;//默认最后一个
//根据Value值设置
DropDownList1.SelectedValue = "";//默认选中项的Code为002 //根据Text值设置
foreach (ListItem li in DropDownList1.Items)
{
if (li.Text == "桓台")
{
li.Selected = true;
}
}
3、取出数据
1 Label1.Text = DropDownList1.SelectedValue;//取出Value值
Label1.Text = DropDownList1.SelectedItem.Text;//取出Text值
Label1.Text = DropDownList1.SelectedItem.Selected.ToString();//获取一个值判定是否被选中
ListBox 复选列表
属性: SelectionMode 列表选择模式: Single 单选;Multiple多选
绑定数据,设置选中项,取出数据方法同CheckBoxList相同。