如何在Checkbox的CheckedChanged事件中获取repeater-item? [英] How to get the repeater-item in a Checkbox' CheckedChanged-event?

查看:422
本文介绍了如何在Checkbox的CheckedChanged事件中获取repeater-item?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在转发器中有一个复选框。像这样:

I have a CheckBox inside a Repeater. Like this:

<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 发生我想得到 Text 标签 lblCampCode

When some one clicks on the checkbox I want to get that entire row in my code behind. So if a CheckedChanged happens I want to get the Text of the Label lblCampCode in code behind.

有可能吗?

我已经设法写了这么多的代码。

I have managed to write this much code.

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
}

$ b $所以你想得到 RepeaterItem

推荐答案

你可以通过转换 CheckBox (sender参数)的 NamingContainer 来实现。然后您就快到了,您需要 FindControl 的标签:

So you want to get the RepeaterItem? You do that by casting the NamingContainer of the CheckBox(the sender argument). Then you're almost there, you need FindControl for the label:

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 - 认为它工作,即使你添加其他容器控件如面板

This has the big advantage over Parent.Parent-approaches that it works even if you add other container controls like Panel or Table.

顺便说一下, ASP.NET中的数据绑定Web控制(如 GridView 等)。

By the way, this works the similar way for any databound web-control in ASP.NET (like GridView etc).

这篇关于如何在Checkbox的CheckedChanged事件中获取repeater-item?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆