通过复选框获取转发器行数据

时间:2014-12-01 12:33:16

标签: c# asp.net repeater datarow asprepeater

我有一个转发器控件和每行一个复选框。在复选框单击事件上,我想获取此行的数据项。我尝试了以下内容,但Repeater项目中的数据项始终为空。

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 个答案:

答案 0 :(得分:1)

试试这个:

如果CheckBox位于Repeater内,则

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

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

此处您可以通过投放RepeaterItem&#39; CheckBox来获取NamingContainer。然后,您可以使用FindControl获取对其他内部控件Repeater

的引用