Repeater nested into a Repeater causing CheckBox.OnCheckedChanged event trigger unexpectingly

China☆狼群 提交于 2019-12-25 04:55:16

问题


I am working on an ASP WebForm.

The page contains a father ASP:Repeater. This Repeater's children themselves contain a Repeater that contains a collection of CheckBoxes. I need to add an event on these CheckBoxes OnCheckChange so whenever we click one of the CheckBoxes, it unchecks all of the other checkboxes. I could use a RadioButton, but using CheckBoxes has other advantages for me that are out of this subject so I do not want to use radio buttons.

That would theorically work fine just like this:

<asp:Repeater ID="FatherContainer" runat="server" OnItemDataBound="On_FatherContainer_ItemDataBound">

    <ItemTemplate>

        ...

        <ASP:Repeater id="ChildRepeater" runat="server" OnItemDataBound="MyRepeater_ItemDataBound">
            <ItemTemplate>
                <asp:CheckBox id="MyCB" runat="server" OnCheckedChanged="MyCB_CheckedChanged" AutoPostBack="true" />
            </ItemTemplate>
        </ASP:Repeater>

    </ItemTemplate>

</ASP:Repeater>

I want that, when I click on a CB, the following method triggers:

protected void MyCB_CheckedChanged(object sender, EventArgs e)
{
    UncheckallOtherCheckBoxes();
}

I must have some binding issues that cause all scenarios not to work.

If I data bind my FatherContainer into Page_Load on first load only:

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        MyRepeater.DataSource = DS;
        MyRepeater.DataBind();
    }
}

the data binding is fine on first load and I can see all my beloved checkboxes. But when I click any CheckBox, the page reloads, FatherContainer does not re-bind, the checkboxes cease to exist and their event method never triggers.

So I take it that I have to DataBind on every page load that way:

protected void Page_Load(object sender, EventArgs e)
{
//  if (!IsPostBack)
    {
        MyRepeater.DataSource = DS;
        MyRepeater.DataBind();
    }
}

If I do that, I get some unexpected results.

Let's say my first ItemTemplate contains 3 CheckBoxes.

On first load, everything is fine again.

If I click on the second CB, it does reload, FatherContainer rebinds into Page_Load, and then MyCB_CheckedChanged is fired with sender = my second CB. That is normal so far.

Now if I click on the third CB, the page reloads again, FatherContainer rebinds again. MyCB_CheckedChanged is fired twice!!! First time with SENDER = SECOND CB again!! and second time with 3rd CB.

And if I click CB2 again, on page reload, MyCB_CheckedChanged fires with sender = CB3 (and not CB2).

It looks that MyCB_CheckedChanged does not trigger when a CB status changes but when a CB is checked after Page_Load.

I tried to databind into Page_Init instead : same results.

And if I dataBind into OnPreRender, MyCB_CheckedChanged is never triggered at all!!

So what shoud I do to have MyCB_CheckedChanged being fired only for the CB that was just checked or unchecked?

Thx in advance.

来源:https://stackoverflow.com/questions/41922676/repeater-nested-into-a-repeater-causing-checkbox-oncheckedchanged-event-trigger

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!