如何在复选框的CheckedChanged事件中获取重复项?

5

我有一个 CheckBox 在一个 Repeater 中。就像这样:

<asp:Repeater ID="rptEvaluationInfo" runat="server">
    <ItemTemplate>
       <asp:Label runat="server" Id="lblCampCode" Text="<%#Eval("CampCode") %>"></asp:Label>
       <asp:CheckBox runat="server" ID="cbCoaching" value="coaching-required" ClientIDMode="AutoID" AutoPostBack="True" OnCheckedChanged="cbCoaching_OnCheckedChanged" />
    </ItemTemplate>
</asp:Repeater>

当有人点击复选框时,我希望在我的代码后台中获取整行数据。所以如果发生CheckedChanged事件,我想要在代码后台中获取标签lblCampCode的文本内容。这是否可能?
我已经成功编写了以下代码。
protected void cbCoaching_OnCheckedChanged(object sender, EventArgs e)
{
    CheckBox chk = (CheckBox)sender;
    Repeater rpt = (Repeater)chk.Parent.Parent;
    string CampCode = "";//  here i want to get the value of CampCode in that row
}
1个回答

13

所以你想获取RepeaterItem?你需要将CheckBox(发送器参数)的NamingContainer转换。然后,你就快要完成了,你需要FindControl用于标签:

protected void cbCoaching_OnCheckedChanged(object sender, EventArgs e)
{
    CheckBox chk = (CheckBox)sender;
    RepeaterItem item = (RepeaterItem) chk.NamingContainer;
    Label lblCampCode = (Label) item.FindControl("lblCampCode");
    string CampCode = lblCampCode.Text;//  here i want to get the value of CampCode in that row
}

相比于“Parent.Parent”方法,这种方法的最大优点是,即使您添加其他容器控件,例如PanelTable,也可以正常工作。

顺便说一下,在ASP.NET中,任何数据绑定的Web控件(如GridView等)都可以以类似的方式工作。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接