The sample code at the front-end is as follows. The datalist control is used to display StudentID and name in the Student table.
Copy codeThe Code is as follows:
<Asp: DataList ID = "dlTable" runat = "server">
<ItemTemplate>
<Td align = "center">
<Asp: Label ID = "lblID" runat = "server" Text = '<% # Eval ("StudentID") %> 'visible = "false"> </asp: label>
<Asp: CheckBox ID = "Chkbox" runat = "server"/>
</Td>
<Td>
<% # Eval ("studentname") %>
</Td>
<Td>
<% # Eval ("studentid") %>
</Td>
</ItemTemplate>
</Asp: DataList>
The background code is as follows:
Copy codeThe Code is as follows:
// New stringbulider instance sbitems
StringBuilder sbitems = new StringBuilder ();
Foreach (DataListItem item in dlTable. Items)
{
// Obtain the foreground Control
CheckBox chkbox = item. FindControl ("Chkbox") as CheckBox;
Label lbId = item. FindControl ("lblID") as Label;
// If the checkbox is checked, assign the value bound to the corresponding label to sbitems for subsequent operations, such as deletion.
If (chkbox = null | lblID = null)
{
Continue;
}
If (chkbox. Checked)
{
Sbitems. Append (lblID. Text. ToString ());
Sbitems. Append (",");
}
}
// Remove the comma because a comma is added.
If (! Sbitems. ToString (). Trim (). Equals (string. Empty ))
{
Sbitems. Remove (sbitems. Length-1, 1 );
}
In this way, the value selected in the front-end checkbox is obtained, but I feel that this method is a little more complicated. If you can better use C # To obtain the Checkbox value method, please enlighten me. I 'd like to know how to use the Checkboxlist method.