ListBox控件:
概述:用于显示一组列表项,用户可以选择其中的一项或者多项,若列表项太多,则系统自动添加滚动条。
ListBox常用属性:
Items属性:
用于往ListBox中添加数据,我们可以在设计界面去添加
也可以使用代码去添加:
前端:
<asp:ListBox ID="listDay" runat="server" Height="28px" Width="205px">
<asp:ListItem>周日</asp:ListItem>
<asp:ListItem>周一</asp:ListItem>
<asp:ListItem>周二</asp:ListItem>
<asp:ListItem>周三</asp:ListItem>
<asp:ListItem>周四</asp:ListItem>
<asp:ListItem>周五</asp:ListItem>
<asp:ListItem>周六</asp:ListItem>
</asp:ListBox>
后端:
protected void Page_Load(object sender, EventArgs e)
{
listDay.Items.Add("周日");
listDay.Items.Add("周一");
listDay.Items.Add("周二");
listDay.Items.Add("周三");
listDay.Items.Add("周四");
listDay.Items.Add("周五");
listDay.Items.Add("周六");
}
DataSource属性:
通过这个属性,我们可以从数组或集合中获取列表项并将其加到控件中。
protected void Page_Load(object sender, EventArgs e)
{
ArrayList arr = new ArrayList();
arr.Add("周日");
arr.Add("周一");
arr.Add("周二");
arr.Add("周三");
arr.Add("周四");
arr.Add("周五");
arr.Add("周六");
listDay.DataSource = arr;
listDay.DataBind();
}
上边就是三种给ListBox控件添加数据的方法。
DropDownList控件:
概述: 在DropDownList中存储数据以后,用户通过下拉显示的方式显示里边的数据,功能和ListBox类似。
常用属性:
例子:动态改变DropDownList控件的背景颜色
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
string color = this.DropDownList1.SelectedValue;
switch (color)
{
case "Red":
this.DropDownList1.BackColor = System.Drawing.Color.Red;
break;
case "Green":
this.DropDownList1.BackColor = System.Drawing.Color.Green;
break;
case "Blue":
this.DropDownList1.BackColor = System.Drawing.Color.Blue;
break;
case "LightGray":
this.DropDownList1.BackColor = System.Drawing.Color.LightGray;
break;
default:
this.DropDownList1.BackColor = System.Drawing.Color.White;
break;
}
}
RadioButton控件:
概述:单选按钮,用来强制用户在所给的选项集中选择其中的一个。
常用属性:
GroupName:用于指定某一个单选框是属于哪一组的,如果不标明单选框是哪一个组,会导致每个单选框都可以被选中。
Checked:用来表示某一个单选按钮是否被选中。
TextAlign:用来设置文字在单选按钮的左边还是右边,值为:Left和Right
CheckBox控件:
概述:允许用户一次选择多个的复选框。
常用属性: