asp.net gets a summary of the values of Checkbox in Datalist

  • 2020-05-09 18:27:41
  • OfStack

The example code for the foreground is shown below, using the datalist control to display the StudentID and name in the Student table
 
<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:
 
//new1 a stringbulider An instance of the sbitems 

StringBuilder sbitems = new StringBuilder(); 
foreach (DataListItem item in dlTable.Items) 
{ 

// Get the foreground control  
CheckBox chkbox = item.FindControl("Chkbox") as CheckBox; 
Label lbId = item.FindControl("lblID") as Label; 

// if checkbox be check Ok, so that will correspond label The value of the binding is assigned to sbitems , for subsequent operations, such as deletion.  
if (chkbox == null || lblID== null) 
{ 
continue; 
} 
if (chkbox.Checked) 
{ 
sbitems.Append(lblID.Text.ToString()); 
sbitems.Append(","); 
} 
} 

// Due to the much 1 A comma, so more remove Out a  
if (!sbitems.ToString().Trim().Equals(string.Empty)) 
{ 
sbitems.Remove(sbitems.Length - 1, 1); 
} 

This gets the value selected from the foreground checkbox, but I feel that this method is a bit too complicated to use. If you have a better way to get Checkbox via C#, please comment. I would like to know how to use Checkboxlist.

Related articles: