您的位置:首页 > 编程语言 > ASP

Web Server Controls->ASP.NET CheckBoxList Control

2007-03-27 09:05 477 查看

Definition and Usage

The CheckBoxList control is used to create a multi-selection check box group.

Each selectable item in a CheckBoxList control is defined by a ListItem element!

Tip: This control supports data binding!

Properties

PropertyDescription
AutoPostBackA Boolean value that specifies whether the form should be posted immediately after one of the items changes select status or not. Default is false
CellPaddingThe space, in pixels, between the cell walls and the check box group
DataSourceThe data source to use
DataTextFieldA field in the data source to be displayed in the check box group
DataValueFieldA field in the data source that specifies the value of each selectable item in the check box group
idA unique id for the control
OnSelectedIndexChangedThe name of the function to be executed when one of the items changes select status
RepeatColumnsThe number of columns to use when displaying the check box group. Default is "1"
RepeatDirectionSpecifies whether the check box group should be repeated horizontally or vertically. Legal values are "Horizontal" and "Vertical". Default is Vertical
RepeatLayoutThe layout of the check box group. Can be "Table" or "Flow". Default is Table
runatSpecifies that the control is a server control. Must be set to "server"
TextAlignOn which side of the check box the text should appear (right or left)

Examples

CheckBoxList
ASPX Source:

<script runat="server">
Sub Check(sender As Object, e As EventArgs)
dim i
mess.Text="<p>Selected Item(s):</p>"
for i=0 to check1.Items.Count-1
if check1.Items(i).Selected then
mess.Text+=check1.Items(i).Text + "<br />"
end if
next
End Sub
</script>

<html>
<body>

<form runat="server">
<asp:CheckBoxList id="check1" AutoPostBack="True"
TextAlign="Right" OnSelectedIndexChanged="Check"
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>
<br />
<asp:label id="mess" runat="server"/>
</form>

</body>
</html>


Output Result:



Item 1
Item 2
Item 3
Item 4
Item 5
Item 6
IF you click the checkboxlist "Item 1",it will shows:

Item 1
Item 2
Item 3
Item 4
Item 5
Item 6
Selected Item(s):

Item 1
In this example we declare one CheckBoxList control in an .aspx file. Then we create an event handler for the SelectedIndexChanged event. The selectable list contains six check boxes. When a user checks one of the boxes, the page is immediately posted back to the server, and the Check subroutine is executed. The subroutine loops through the control's Items collection and tests each item's Selected property. The selected items are then displayed in the Label control.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: