CheckBoxList Web 服务器控件声明性语法
更新:2007 年 11 月
创建多选复选框组。此控件支持绑定到数据源。
<asp:CheckBoxList
AccessKey="string"
AppendDataBoundItems="True|False"
AutoPostBack="True|False"
BackColor="color name|#dddddd"
BorderColor="color name|#dddddd"
BorderStyle="NotSet|None|Dotted|Dashed|Solid|Double|Groove|Ridge|
Inset|Outset"
BorderWidth="size"
CausesValidation="True|False"
CellPadding="integer"
CellSpacing="integer"
CssClass="string"
DataMember="string"
DataSource="string"
DataSourceID="string"
DataTextField="string"
DataTextFormatString="string"
DataValueField="string"
Enabled="True|False"
EnableTheming="True|False"
EnableViewState="True|False"
Font-Bold="True|False"
Font-Italic="True|False"
Font-Names="string"
Font-Overline="True|False"
Font-Size="string|Smaller|Larger|XX-Small|X-Small|Small|Medium|
Large|X-Large|XX-Large"
Font-Strikeout="True|False"
Font-Underline="True|False"
ForeColor="color name|#dddddd"
Height="size"
ID="string"
OnDataBinding="DataBinding event handler"
OnDataBound="DataBound event handler"
OnDisposed="Disposed event handler"
OnInit="Init event handler"
OnLoad="Load event handler"
OnPreRender="PreRender event handler"
OnSelectedIndexChanged="SelectedIndexChanged event handler"
OnTextChanged="TextChanged event handler"
OnUnload="Unload event handler"
RepeatColumns="integer"
RepeatDirection="Horizontal|Vertical"
RepeatLayout="Table|Flow"
runat="server"
SelectedIndex="integer"
SelectedValue="string"
SkinID="string"
Style="string"
TabIndex="integer"
TextAlign="Left|Right"
ToolTip="string"
ValidationGroup="string"
Visible="True|False"
Width="size"
>
<asp:ListItem
Enabled="True|False"
Selected="True|False"
Text="string"
Value="string"
/>
</asp:CheckBoxList>
备注
CheckBoxList 控件创建可使用数据绑定动态生成的多选复选框组。若要指定要在 CheckBoxList 控件中显示的项,请针对每项在 CheckBoxList 控件的开始标记和结束标记之间放置一个 ListItem 元素。
![]() |
---|
还可以使用多个 CheckBox 控件。对于使用数据绑定创建一组复选框而言,CheckBoxList 控件更易于使用,而各个 CheckBox 控件则使您可以更好地控制布局。 |
CheckBoxList 控件还支持数据绑定。若要将该控件绑定到数据源,请首先创建一个数据源(如 DataSourceControl 对象)以包含要在该控件中显示的项。下一步,使用 DataBind 方法将该数据源绑定到 CheckBoxList 控件。使用 DataTextField 和 DataValueField 属性分别指定将数据源中的哪个字段绑定到控件中每个列表项的 Text 和 Value 属性。现在,CheckBoxList 控件将显示数据源中的信息。
若要确定 CheckBoxList 控件中的选定项,请循环访问 Items 集合并测试该集合中每一项的 Selected 属性。
可以使用 RepeatLayout 和 RepeatDirection 属性指定列表的显示方式。如果 RepeatLayout 设置为 RepeatLayout.Table(默认设置),则该列表呈现在一个表内。如果它被设置为 RepeatLayout.Flow,则该列表在呈现时没有任何表结构。默认情况下,RepeatDirection 设置为 RepeatDirection.Vertical。将此属性设置为 RepeatDirection.Horizontal 可以在水平方向上呈现该列表。
![]() |
---|
文本在 CheckBoxList 控件中显示之前并非 HTML 编码形式。这使得可以在文本中的 HTML 标记中嵌入脚本。如果控件的值是由用户输入的,请务必要对输入值进行验证以防止出现安全漏洞。 |
有关 CheckBoxList Web 服务器控件的属性和事件的详细信息,请参见 CheckBoxList 类文档。
示例
以下内容显示在 .aspx 文件中声明 CheckBoxList 控件的示例。该列表包含六个不相互排斥的项。当用户选中某个框时,该页不会立即发送回服务器(直到发生某个其他事件(如 Button 单击)时才会发送该页)。由于没有为 OnSelectedIndexChanged 事件声明任何方法,因此不会调用该事件的任何事件处理程序。
<asp:CheckBoxList id="Check1"
RepeatLayout="flow"
runat="server">
<asp:ListItem>Item 1</asp:ListItem>
<asp:ListItem>Item 2</asp:ListItem>
<asp:ListItem>Item 3</asp:ListItem>
<asp:ListItem>Item 4</asp:ListItem>
<asp:ListItem>Item 5</asp:ListItem>
<asp:ListItem>Item 6</asp:ListItem>
</asp:CheckBoxList>
<asp:CheckBoxList id="Check1"
RepeatLayout="flow"
runat="server">
<asp:ListItem>Item 1</asp:ListItem>
<asp:ListItem>Item 2</asp:ListItem>
<asp:ListItem>Item 3</asp:ListItem>
<asp:ListItem>Item 4</asp:ListItem>
<asp:ListItem>Item 5</asp:ListItem>
<asp:ListItem>Item 6</asp:ListItem>
</asp:CheckBoxList>
下面的示例演示如何确定 CheckBoxList 控件中的选定复选框。这段代码循环访问控件的 Items 集合,并测试每一项的 Selected 属性。然后,选定项便显示在 Label 控件中。
Sub Button1_OnClick(ByVal sender As Object, ByVal e As EventArgs)
Dim s As String
Dim i As Integer
s = "Selected items:<br />"
For i = 0 To Check1.Items.Count - 1
If Check1.Items(i).Selected Then
s = s & Check1.Items(i).Text & "<br />"
End If
Next i
Label1.Text = s
End Sub
void Button1_OnClick(Object sender, EventArgs e)
{
string s;
s = "Selected items:<br />";
for (int i = 0; i < Check1.Items.Count; i++)
{
if (Check1.Items[i].Selected)
{
s = s + Check1.Items[i].Text + "<br />";
}
}
Label1.Text = s;
}