dimanche 29 janvier 2017

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

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:

    <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.




Aucun commentaire:

Enregistrer un commentaire