ASP.NET Syntax ASP.NET Syntax for Web Controls
Creates a multi-selection check box group that can be dynamically generated by using data binding.
Declarative Syntax
<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>
For information on the individual members of this class, see CheckBoxList in the class library.
The CheckBoxList control renders a multi-selection check box group that can be dynamically generated by using data binding. It contains an Items collection with members that correspond to individual items in the list. To determine which items are checked, loop through and test the Selected property of each item in the list.
You can specify the way the list is displayed by using the RepeatLayout and RepeatDirection properties. If RepeatLayout is set to RepeatLayout.Table ( the default setting ), the list is rendered within a table. If it is set to RepeatLayout.Flow, the list is rendered without any table structure. By default, RepeatDirection is set to RepeatDirection.Vertical. Setting this property to RepeatDirection.Horizontal renders the list horizontally.
NOTE: You can also use multiple CheckBox controls. The CheckBoxList control is easier for creating a set of check boxes using data binding, while the individual CheckBox control gives you greater control over layout.
The following shows a sample declaration for a CheckBoxList control in an .aspx file. The list contains items that are not mutually exclusive. In this case, the page is not immediately posted back to the server when a user checks one of the boxes, until some other event occurs, such as a Button click ).
<asp:CheckBoxList id="CheckList1" runat="server">
<asp:ListItem>Item 1</asp:ListItem>
...
<asp:ListItem>Item n</asp:ListItem>
</asp:CheckBoxList>
The following example shows how you can determine which check boxes are selected in a CheckBoxList control. The code loops through the control’s Items collection, and for each item, tests the item’s Selected property.
void Button1_onClick ( Object sender, EventArgs e ) {
string s = "Selected items:<br>";
for ( int i=0; i < CheckList1.Items.Count; i++ ) {
if ( CheckList1.Items [ i ] .Selected ) {
s += CheckList1.Items [ i ] .Text + "<br>";
}
}
msgLabel.Text = s;
}
Sub Button1_onClick ( sender As Object, e As EventArgs )
Dim s As String = "Selected items:<br>"
Dim i As Integer
For i=0 to Check1.Items.Count - 1
If CheckList1.Items ( i ).Selected Then
s = s & CheckList1.Items [ i ] .Text & "<br>"
End If
Next i
msgLabel.Text = s
End Sub |
|
C# |
VB |
CheckBoxList Class CheckBox and CheckBoxList Web Server Controls