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

本文关键字:数据 转发器 获取 复选框 | 更新日期: 2023-09-27 18:32:32

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

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>

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

试试这个:

如果CheckBoxRepeater里面,那么

var chk =  (CheckBox)sender;
var item = (RepeaterItem)chk.NamingContainer;
if (item.ItemType == ListItemType.Item)
{
//find your items here
}

在这里你可以通过施放CheckBoxNamingContainer来获得RepeaterItem。然后,您可以使用 FindControl 获取对内部其他控件的引用Repeater