The sample code for the foreground is as follows, using the DataList control, to display the StudentID and names in the student table
Copy Code code 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 Code code as follows:
New instance of a Stringbulider Sbitems
StringBuilder sbitems = new StringBuilder ();
foreach (DataListItem item in Dltable.items)
{
Get foreground control
CheckBox Chkbox = Item. FindControl ("Chkbox") as CheckBox;
Label lbid = Item. FindControl ("Lblid") as Label;
If the checkbox is check, assign the value of the corresponding label binding to Sbitems for subsequent operations, such as deletion.
if (Chkbox = null | | lblid== null)
{
Continue
}
if (Chkbox. Checked)
{
Sbitems. Append (LblID.Text.ToString ());
Sbitems. Append (",");
}
}
Because I have a comma, I want to remove the comma.
if (!sbitems. ToString (). Trim (). Equals (String. Empty))
{
Sbitems. Remove (Sbitems. Length-1, 1);
}
This gets the value selected in the Foreground checkbox, but I feel like this method is a little bit more complicated, you guys. If there is a better way to get a checkbox through C #, please enlighten me. I am very interested to know how to use the CheckBoxList method.