Get checked CheckBox Row values of ASP.Net Repeater using C# and VB.Net

itsjayshah
 
on Feb 12, 2019 12:08 AM
Sample_165045.zip
14183 Views

How to get selected row value  from repeater on button click.

button is out side of repeater

<asp:Repeater ID="rptCustomers" runat="server">
    <ItemTemplate>
        <label class="switch">
            <asp:CheckBox runat="server" ID="rbt_etails" Text='<%# Eval("xyz") %>' ClientIDMode="Static" />
            <span class="slider round"></span>
        </label>
    </ItemTemplate>
</asp:Repeater>

 

Download FREE API for Word, Excel and PDF in ASP.Net: Download
pandeyism
 
on Feb 12, 2019 03:05 AM

Hi itsjayshah,

Please refer below sample.

HTML

<asp:Repeater ID="rptCustomers" runat="server">
    <ItemTemplate>
        <label class="switch">
            <asp:CheckBox runat="server" ID="rbt_etails" Text='<%# Eval("ID") %>' ClientIDMode="Static" />
            <span class="slider round"></span>
        </label>
    </ItemTemplate>
</asp:Repeater>
<asp:Button ID="Button1" Text="GetValue" runat="server" OnClick="getCheckedRow" />
<asp:Label ID="lblMessage" runat="server" />

Namespaces

C#

using System.Data;

VB.Net

Imports System.Data

Code

C#

protected void Page_Load(object sender, EventArgs e)
{
    if (!this.IsPostBack)
    {
        DataTable dt = new DataTable();
        dt.Columns.Add("ID");
        dt.Rows.Add(1);
        dt.Rows.Add(2);
        dt.Rows.Add(3);
        rptCustomers.DataSource = dt;
        rptCustomers.DataBind();
    }
}

protected void getCheckedRow(object sender, EventArgs e)
{
    string value = "";
    foreach (RepeaterItem item in rptCustomers.Items)
    {
        CheckBox chk = item.FindControl("rbt_etails") as CheckBox;
        if (chk.Checked)
        {
            value += chk.Text + ",";

        }
    }
    lblMessage.Text = "SELECT VALUE : <b>" + value.Trim(',', ' ') + "</b>";
}

VB.Net

Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
    If Not Me.IsPostBack Then
        Dim dt As DataTable = New DataTable()
        dt.Columns.Add("ID")
        dt.Rows.Add(1)
        dt.Rows.Add(2)
        dt.Rows.Add(3)
        rptCustomers.DataSource = dt
        rptCustomers.DataBind()
    End If
End Sub

Protected Sub getCheckedRow(ByVal sender As Object, ByVal e As EventArgs)
    Dim value As String = ""

    For Each item As RepeaterItem In rptCustomers.Items
        Dim chk As CheckBox = TryCast(item.FindControl("rbt_etails"), CheckBox)

        If chk.Checked Then
            value += chk.Text & ","
        End If
    Next

    lblMessage.Text = "SELECT VALUE : <b>" & value.Trim(","c, " "c) & "</b>"
End Sub

Screenshot