Find RadioButton inside nested Repeater on Parent Repeater Button Click using C# and VB.Net in ASP.Net

Last Reply one month ago By dharmendr

Posted one month ago

I have a Repeater control inside template i have a button and another repeater with radiobutton not radiobutton list.

When i select an radiobutton in it and click parent repeater button the selected radiobutton is lost.

What will be the issue.

<Repeater>
    <itemtemplate>
        <Repeater>
            <itemtemplate>
                <RadioButton/>
                <RadioButton/>
            </itemtemplate>
        </Repeater>
        <Button/>
    </itemtemplate>
</Repeater>

 

You are viewing reply posted by: dharmendr one month ago.
Posted one month ago

Hi rani,

Check this example. Now please take its reference and correct your code.

HTML

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title></title>
    <style type="text/css">
        body
        {
            font-family: Arial;
            font-size: 10pt;
        }
        .Grid, .ChildGrid
        {
            border: 1px solid #ccc;
        }
        .Grid td, .Grid th
        {
            border: 1px solid #ccc;
        }
        .Grid th
        {
            background-color: #B8DBFD;
            color: #333;
            font-weight: bold;
        }
        .ChildGrid td, .ChildGrid th
        {
            border: 1px solid #ccc;
        }
        .ChildGrid th
        {
            background-color: #ccc;
            color: #333;
            font-weight: bold;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <asp:Repeater ID="rptCustomers" runat="server" OnItemDataBound="OnItemDataBound">
        <HeaderTemplate>
            <table class="Grid" cellspacing="0" rules="all" border="1">
                <tr>
                    <th scope="col">&nbsp;</th>
                    <th scope="col" style="width: 150px">Name</th>
                    <th scope="col" style="width: 150px">Country</th>
                    <th scope="col" style="width: 150px">Action</th>
                </tr>
        </HeaderTemplate>
        <ItemTemplate>
            <tr>
                <td>
                    <img alt="" style="cursor: pointer" src="images/plus.png" />
                    <asp:Panel ID="pnlOrders" runat="server" Style="display: none">
                        <asp:Repeater ID="rptOrders" runat="server">
                            <HeaderTemplate>
                                <table class="ChildGrid" cellspacing="0" rules="all" border="1">
                                    <tr>
                                        <th scope="col" style="width: 150px">Order Id</th>
                                        <th scope="col" style="width: 150px"></th>
                                    </tr>
                            </HeaderTemplate>
                            <ItemTemplate>
                                <tr>
                                    <td><asp:Label ID="lblOrderId" runat="server" Text='<%# Eval("OrderId") %>' /></td>
                                    <td>
                                        <asp:RadioButton ID="rbYes" Text="Yes" runat="server" GroupName="A" />
                                        <asp:RadioButton ID="rbNo" Text="No" runat="server" GroupName="A" />
                                    </td>
                                </tr>
                            </ItemTemplate>
                            <FooterTemplate>
                                </table>
                            </FooterTemplate>
                        </asp:Repeater>
                    </asp:Panel>
                    <asp:HiddenField ID="hfCustomerId" runat="server" Value='<%# Eval("Id") %>' />
                </td>
                <td><asp:Label ID="lblContactName" runat="server" Text='<%# Eval("Name") %>' /></td>
                <td><asp:Label ID="lblCity" runat="server" Text='<%# Eval("Country") %>' /></td>
                <td><asp:Button ID="btnGet" Text="Get Values" runat="server" OnClick="GetSelected" /></td>
            </tr>
        </ItemTemplate>
        <FooterTemplate>
            </table>
        </FooterTemplate>
    </asp:Repeater>
    <br />
    <asp:Label ID="lblMessage" runat="server" />
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
    <script type="text/javascript">
        $("body").on("click", "[src*=plus]", function () {
            $(this).closest("tr").after("<tr><td></td><td colspan = '999'>" + $(this).next().html() + "</td></tr>")
            $(this).attr("src", "images/minus.png");
        });
        $("body").on("click", "[src*=minus]", function () {
            $(this).attr("src", "images/plus.png");
            $(this).closest("tr").next().remove();
        });
    </script>
    </form>
</body>
</html>

Namespaces

C#

using System.Data;

VB.Net

Imports System.Data

Code

C#

protected void Page_Load(object sender, EventArgs e)
{
    if (!this.IsPostBack)
    {
        rptCustomers.DataSource = GetCustomers();
        rptCustomers.DataBind();
    }
}

private static DataTable GetCustomers()
{
    DataTable dt = new DataTable();
    dt.Columns.AddRange(new DataColumn[] { 
                        new DataColumn("Id", typeof(int)),
                        new DataColumn("Name", typeof(string)),
                        new DataColumn("Country",typeof(string)) });
    dt.Rows.Add(1, "John Hammond", "United States");
    dt.Rows.Add(2, "Mudassar Khan", "India");
    dt.Rows.Add(3, "Suzanne Mathews", "France");
    dt.Rows.Add(4, "Robert Schidner", "Russia");
    return dt;
}

private static DataTable GetOrders()
{
    DataTable dt = new DataTable();
    dt.Columns.AddRange(new DataColumn[] { 
                        new DataColumn("OrderId", typeof(int)),
                        new DataColumn("CustomerId", typeof(int)) });
    dt.Rows.Add(1001, 1);
    dt.Rows.Add(1005, 1);
    dt.Rows.Add(1002, 2);
    dt.Rows.Add(1003, 3);
    dt.Rows.Add(1004, 4);
    return dt;
}

protected void OnItemDataBound(object sender, RepeaterItemEventArgs e)
{
    if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
    {
        string customerId = (e.Item.FindControl("hfCustomerId") as HiddenField).Value;
        Repeater rptOrders = e.Item.FindControl("rptOrders") as Repeater;
        rptOrders.DataSource = GetOrders().Select("CustomerId=" + customerId).CopyToDataTable();
        rptOrders.DataBind();
    }
}

protected void GetSelected(object sender, EventArgs e)
{
    lblMessage.Text = "";
    Button btn = sender as Button;
    foreach (RepeaterItem item in (btn.FindControl("rptOrders") as Repeater).Items)
    {
        if (item.ItemType == ListItemType.AlternatingItem || item.ItemType == ListItemType.Item)
        {
            Label orderId = (Label)item.FindControl("lblOrderId");
            RadioButton rbYes = (RadioButton)item.FindControl("rbYes");
            RadioButton rbNo = (RadioButton)item.FindControl("rbNo");
            string selected = rbYes.Checked ? rbYes.Text : rbNo.Text;
            lblMessage.Text += "Order " + orderId.Text + " is " + selected + "<br/>";
        }
    }
}

VB.Net

Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
    If Not Me.IsPostBack Then
        rptCustomers.DataSource = GetCustomers()
        rptCustomers.DataBind()
    End If
End Sub

Private Shared Function GetCustomers() As DataTable
    Dim dt As DataTable = New DataTable()
    dt.Columns.AddRange(New DataColumn() {New DataColumn("Id", GetType(Integer)),
                                           New DataColumn("Name", GetType(String)),
                                           New DataColumn("Country", GetType(String))})
    dt.Rows.Add(1, "John Hammond", "United States")
    dt.Rows.Add(2, "Mudassar Khan", "India")
    dt.Rows.Add(3, "Suzanne Mathews", "France")
    dt.Rows.Add(4, "Robert Schidner", "Russia")
    Return dt
End Function

Private Shared Function GetOrders() As DataTable
    Dim dt As DataTable = New DataTable()
    dt.Columns.AddRange(New DataColumn() {
                        New DataColumn("OrderId", GetType(Integer)),
                        New DataColumn("CustomerId", GetType(Integer))})
    dt.Rows.Add(1001, 1)
    dt.Rows.Add(1005, 1)
    dt.Rows.Add(1002, 2)
    dt.Rows.Add(1003, 3)
    dt.Rows.Add(1004, 4)
    Return dt
End Function

Protected Sub OnItemDataBound(ByVal sender As Object, ByVal e As RepeaterItemEventArgs)
    If e.Item.ItemType = ListItemType.Item OrElse e.Item.ItemType = ListItemType.AlternatingItem Then
        Dim customerId As String = (TryCast(e.Item.FindControl("hfCustomerId"), HiddenField)).Value
        Dim rptOrders As Repeater = TryCast(e.Item.FindControl("rptOrders"), Repeater)
        rptOrders.DataSource = GetOrders().[Select]("CustomerId=" & customerId).CopyToDataTable()
        rptOrders.DataBind()
    End If
End Sub

Protected Sub GetSelected(ByVal sender As Object, ByVal e As EventArgs)
    lblMessage.Text = ""
    Dim btn As Button = TryCast(sender, Button)
    For Each item As RepeaterItem In (TryCast(btn.FindControl("rptOrders"), Repeater)).Items
        If item.ItemType = ListItemType.AlternatingItem OrElse item.ItemType = ListItemType.Item Then
            Dim orderId As Label = CType(item.FindControl("lblOrderId"), Label)
            Dim rbYes As RadioButton = CType(item.FindControl("rbYes"), RadioButton)
            Dim rbNo As RadioButton = CType(item.FindControl("rbNo"), RadioButton)
            Dim selected As String = If(rbYes.Checked, rbYes.Text, rbNo.Text)
            lblMessage.Text += "Order " & orderId.Text & " is " & selected & "<br/>"
        End If
    Next
End Sub

Screenshot