This sample code will help you to find the CheckBoxList selected values. This code loop through all the checkbox values using foreach and assign comma separated selected values in to label.
Source code:
<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
protected void btnGetSelected_Click(object sender, EventArgs e)
{
lblSelected.Text = "Your selected check box values are: <br>";
foreach (ListItem lst in chkList.Items)
{
if (lst.Selected == true)
{
lblSelected.Text += lst.Text + ", ";
}
}
}
</script>
<html>
<head>
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:CheckBoxList ID="chkList" runat="server">
<asp:ListItem>IndiHub.com</asp:ListItem>
<asp:ListItem>BharatClick.com</asp:ListItem>
<asp:ListItem>SharePointBank.com</asp:ListItem>
<asp:ListItem>SoftwareTestingNet.com</asp:ListItem>
</asp:CheckBoxList>
<br />
<asp:Button ID="btnGetSelected" runat="server" Text="Get Selected Values" OnClick="btnGetSelected_Click" />
<br />
<asp:Label ID="lblSelected" runat="server" Text=""></asp:Label>
</div>
</form>
</body>
</html>
How to remove last comma ?
ReplyDeleteuse lblSelected.Text = lblSelected.Text.TrimEnd(',');
ReplyDelete