In this article I will explain with an example, how to get the Text and Value of selected (checked) items CheckBoxList on Button Click in ASP.Net using C# and VB.Net.
When the Button is clicked, the Text and Value of selected (checked) items CheckBoxList will be fetched and will be displayed using JavaScript Alert Message Box.
HTML Markup
The following HTML Markup consists of an ASP.Net CheckBoxList control and a Button.
Select Fruit:
<asp:CheckBoxList ID="chkFruits" runat="server">
<asp:ListItem Text="Apple" Value="1" />
<asp:ListItem Text="Mango" Value="2" />
<asp:ListItem Text="Papaya" Value="3" />
<asp:ListItem Text="Banana" Value="4" />
<asp:ListItem Text="Orange" Value="5" />
</asp:CheckBoxList>
<asp:Button ID="btnSubmit" Text="Submit" runat="server" OnClick = "Submit" />
Get Text and Value of Selected Items of CheckBoxList on Button Click
Inside the Click event handler, the Text and Value part of all the Selected Items of the CheckBoxList are fetched and displayed using JavaScript Alert Message Box.
C#
protected void Submit(object sender, EventArgs e)
{
string message = "Texts Values";
foreach (ListItem item in chkFruits.Items)
{
if (item.Selected)
{
message += "\\n";
message += item.Text + " " + item.Value; ;
}
}
ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('" + message + "');", true);
}
VB.Net
Protected Sub Submit(ByVal sender As Object, ByVal e As EventArgs)
Dim message As String = "Texts Values"
For Each item As ListItem In chkFruits.Items
If item.Selected Then
message &= "\n"
message &= item.Text & " " + item.Value
End If
Next
ClientScript.RegisterStartupScript(Me.GetType(), "alert", "alert('" & message & "');", True)
End Sub
Screenshot
Demo
Downloads