Get repeater row data through checkbox

1.4k views Asked by At

I have a repeater control and a check box on each row. On the checkbox click event I wanted to obtain the data items for this row. I have tried the following but the data items within Repeater Item is always empty.

protected void CheckChanged(object sender, EventArgs e)
    {
        string county = string.Empty;
        string country = string.Empty;
        string postcode = string.Empty;
        string posttown = string.Empty;

        var item = ((CheckBox) sender).Parent as RepeaterItem;

        if (item != null)
        {
            if (item.ItemType == ListItemType.Item)
            {
                System.Data.DataRowView drv = (System.Data.DataRowView)(item.DataItem);
                county = drv.Row["county"].ToString();
                country = drv.Row["country"].ToString();
                postcode = drv.Row["postcode"].ToString();
                posttown = drv.Row["posttown"].ToString();
            }
        }
    }


<asp:Repeater ID="Repeater1" runat="server">
    <HeaderTemplate>
    <table border="1" width="100%" >
    <tr>
    <th>Select</th>
    <th>Country</th>
    <th>County</th>
    <th>CSS Database Code</th>
    <th>Postcode</th>
    <th>Town</th>
    </tr>
    </HeaderTemplate>
    <Itemtemplate>
        <tr>
            <td><asp:CheckBox ID="selectAddress" runat="server" OnCheckedChanged="CheckChanged" AutoPostBack="true"/></td>
            <td><%# Eval("county")%>
            <td><%# Eval("country")%></td>
            <td><%# Eval("cssDatabaseCode")%></td>
            <td><%# Eval("postcode")%><br /></td>  
            <td><%# Eval("postTown")%><br /></td>
        </tr>
     </Itemtemplate>
    <FooterTemplate>
    </table>
    </FooterTemplate>
    </asp:Repeater>
1

There are 1 answers

4
Dgan On

try this:

If CheckBox is Inside Repeater Then

var chk =  (CheckBox)sender;
var item = (RepeaterItem)chk.NamingContainer;

if (item.ItemType == ListItemType.Item)
{
//find your items here
}

Here You can get the RepeaterItem by casting the CheckBox's NamingContainer. Then you can use FindControl to get the reference to your other Controls Inside Repeater